Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #107378
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Const values not stored in .text section |
| Date | 2017-03-29 15:04 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <obgb7v$5gh$1@dont-email.me> (permalink) |
| References | <391e9782-09dd-43a6-a279-e9b72d6c2cd2@googlegroups.com> |
On 29/03/17 13:31, acidlategamer@gmail.com wrote:
> Hi all,
> I have this knowledge that any const variable declared like this
>
> const int a;
>
> will go to .text section i.e is read only, so we should be able to get its address.
You have jumbled up a variety of concepts here.
Inside a function, this declares a local "int" variable and tells the
compiler that you promise not to change it. Whether the compiler
generates an actual memory local for it or not, is implementation
dependent. And the compiler can legally place it in a variety of
places. A good many local "const" variables never end up in memory at
all - they are optimised away, or they stay in registers. But taking
the address of the variable usually means it will be allocated a space
in memory.
The compiler will usually put it on the stack in a function like your
first one. It is allowed to put it at a static location (such as in the
".text" section, or a ".rodata", ".const"., etc. section) if it knows
all uses of the address of the constant, and knows this is safe - noting
that if a function is called in a re-entrant manner then there will be
at least two separate "const int a" objects which must have different
addresses.
The easiest thing for the compiler, therefore, is to put this "a" on the
stack.
> I tried this,
>
> #include <stdio.h>
> int main()
> {
> const int a = 32;
> int *p;
> p = &a;
> *p = 45;
> printf("%%d p\n", a, (void *)&a);
> return 0;
> }
> compiled gcc -O3 23.c
> output : 23.c: In function ‘main’:
> 23.c:6:3: warning: assignment discards ‘const’ qualifier from pointer target type [enabled by default]
> p = &a;
> ^
You promised the compiler that you would not change "a", then you took a
non-const pointer to it which would allow you to change it. The
compiler helpfully warns you about this bad practice.
> gamer@acid:~/practice_linux/linked_list$ ./a.out
> 45 0x7ffe6ce0f950
>
> So I get a warning and an address from .text section. Am I correct.
> Also the const value got changed.
The address was never in ".text" - "a" would be on the stack.
And the compiler is free to print 32 instead of 45 as the value - or
even 42, or do anything else it fancies since you lied to it.
Typically, if you enable compiler optimisation you will get 32.
>
>
> #include <stdio.h>
> int main()
> {
> const static int a = 32;
> int *p;
> p = &a;
> *p = 45;
> printf("%%d p\n", a, (void *)&a);
> return 0;
> }
>
> output : segmentation fault
>
> What difference does static made here.
> I am using gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
>
By making "a" static, you are telling the compiler that only one of
these exists in the program. Thus the compiler can allocate a single
address for it rather than putting it on the stack. And since it is
"const", the compiler will put it in a read-only memory area (".rodata"
for gcc). When you try to change memory that is protected from writing,
you get a hard fault from the cpu.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Const values not stored in .text section acidlategamer@gmail.com - 2017-03-29 04:31 -0700
Re: Const values not stored in .text section acidlategamer@gmail.com - 2017-03-29 04:41 -0700
Re: Const values not stored in .text section David Brown <david.brown@hesbynett.no> - 2017-03-29 15:04 +0200
Re: Const values not stored in .text section acidlategamer@gmail.com - 2017-03-29 06:14 -0700
Re: Const values not stored in .text section scott@slp53.sl.home (Scott Lurndal) - 2017-03-29 13:41 +0000
Re: Const values not stored in .text section David Brown <david.brown@hesbynett.no> - 2017-03-29 16:19 +0200
Re: Const values not stored in .text section Barry Schwarz <schwarzb@dqel.com> - 2017-03-29 08:06 -0700
Re: Const values not stored in .text section jt@toerring.de (Jens Thoms Toerring) - 2017-03-29 18:38 +0000
Re: Const values not stored in .text section "James R. Kuyper" <jameskuyper@verizon.net> - 2017-03-29 11:49 -0400
Re: Const values not stored in .text section Keith Thompson <kst-u@mib.org> - 2017-03-29 09:55 -0700
Re: Const values not stored in .text section jt@toerring.de (Jens Thoms Toerring) - 2017-03-29 13:34 +0000
Re: Const values not stored in .text section "James R. Kuyper" <jameskuyper@verizon.net> - 2017-03-29 11:45 -0400
csiph-web