Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #104374
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: modifying local const variable run time |
| Date | 2017-02-22 16:06 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <87shn66wgx.fsf@bsb.me.uk> (permalink) |
| References | <c5e88b2a-20a5-4083-a40a-a17d57aa42f7@googlegroups.com> <20170222160611.91c1f3085d6c9e3571a68156@gmail.com> <3cba5f3c-735a-45b7-87f2-d03dd8a18720@googlegroups.com> <o8kaog$9cc$2@dont-email.me> <abdf9730-1fd0-4943-80fc-95c1644ec7fb@googlegroups.com> |
rahul.dev.agg@gmail.com writes:
> On Wednesday, February 22, 2017 at 9:01:28 PM UTC+5:30, David Brown wrote:
>> On 22/02/17 16:26, rahul.dev.agg@gmail.com wrote:
>> > On Wednesday, February 22, 2017 at 8:38:13 PM UTC+5:30, GOTHIER Nathan wrote:
>> >> On Wed, 22 Feb 2017 06:39:51 -0800 (PST)
>> >> rahul.dev.agg@gmail.com wrote:
>> >>
>> >>> int func(void)
>> >>> {
>> >>> const int i = getc();
>> >>> }
>> >>>
>> >>> Is this correct ? Can we modify const local variable run time ?
>> >>
>> >> It should be correct if you don't try to assign a value to your constant
>> >> integer. Constant objects should only be initialized.
>> >
>> > But, how this would be implemented ?
>> > I read that const variable can be stored in read only memory.
>> > In that case, it's value should be known at compile time.
>> > Or is there I am missing something ?
>>
>> A compiler /may/ put a const object in read-only memory, if it knows the
>> initialisation value when the program is compiled. It can't do that for
>> const objects that are initialised at run time. Think of your code here
>> as a saying "I want an "int" called "i", initialised to the result of a
>> call to getc(), and it will not be changed after initialisation".
>
> ok. Great !!! Thanks.
>
> Just curious how this will be implemented ?
The only thing a compiler must do is to report certain cases where i is
used inappropriately. The compiler has to remember that i is const
(just like it must remember that i is an int) so that if, in the same
scope, it sees i++ or i = 99 is can report it. It does not even have to
stop compiling the code. It can generate code to do the operation as if
i were not const. It can refuse to generate a runable binary or it can
do pretty much anything at all since the code now has undefined
behaviour.
C has no way to stop the programmer from modifying an object. Using
const just provides some help in preventing unintended modifications and
it can make the code easier to understand by removing one less thing to
think about.
--
Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
modifying local const variable run time rahul.dev.agg@gmail.com - 2017-02-22 06:39 -0800
Re: modifying local const variable run time GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-02-22 16:06 +0100
Re: modifying local const variable run time rahul.dev.agg@gmail.com - 2017-02-22 07:26 -0800
Re: modifying local const variable run time David Brown <david.brown@hesbynett.no> - 2017-02-22 16:31 +0100
Re: modifying local const variable run time rahul.dev.agg@gmail.com - 2017-02-22 07:43 -0800
Re: modifying local const variable run time Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-22 16:06 +0000
Re: modifying local const variable run time James Kuyper <jameskuyper@verizon.net> - 2017-02-22 11:13 -0500
Re: modifying local const variable run time supercat@casperkitty.com - 2017-02-22 08:34 -0800
Re: modifying local const variable run time David Brown <david.brown@hesbynett.no> - 2017-02-22 23:03 +0100
Re: modifying local const variable run time GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-02-22 17:34 +0100
Re: modifying local const variable run time Robert Wessel <robertwessel2@yahoo.com> - 2017-02-22 10:52 -0600
Re: modifying local const variable run time GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-02-22 17:52 +0100
Re: modifying local const variable run time Keith Thompson <kst-u@mib.org> - 2017-02-22 09:33 -0800
Re: modifying local const variable run time James Kuyper <jameskuyper@verizon.net> - 2017-02-22 15:50 -0500
Re: modifying local const variable run time supercat@casperkitty.com - 2017-02-22 13:13 -0800
Re: modifying local const variable run time Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-22 21:55 +0000
Re: modifying local const variable run time supercat@casperkitty.com - 2017-02-22 14:43 -0800
Re: modifying local const variable run time Keith Thompson <kst-u@mib.org> - 2017-02-22 09:23 -0800
Re: modifying local const variable run time GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-02-22 18:32 +0100
Re: modifying local const variable run time Keith Thompson <kst-u@mib.org> - 2017-02-22 09:50 -0800
Re: modifying local const variable run time David Brown <david.brown@hesbynett.no> - 2017-02-22 23:07 +0100
Re: modifying local const variable run time Keith Thompson <kst-u@mib.org> - 2017-02-22 15:20 -0800
Re: modifying local const variable run time David Brown <david.brown@hesbynett.no> - 2017-02-23 09:50 +0100
Re: modifying local const variable run time James Kuyper <jameskuyper@verizon.net> - 2017-02-22 10:16 -0500
Re: modifying local const variable run time Keith Thompson <kst-u@mib.org> - 2017-02-22 09:17 -0800
csiph-web