Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #107398

Re: Const values not stored in .text section

From "James R. Kuyper" <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: Const values not stored in .text section
Date 2017-03-29 11:45 -0400
Organization A noiseless patient Spider
Message-ID <13ff7fee-c428-e0ef-d604-baa8de8a0b8d@verizon.net> (permalink)
References <391e9782-09dd-43a6-a279-e9b72d6c2cd2@googlegroups.com>

Show all headers | View raw


On 03/29/2017 07:31 AM, 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.

That "knowledge" is implementation-specific. Whether or not an 
implementation even has such a thing as "sections" will vary from one 
platform to another - even implementations that use named sections need 
not use the same names, or for the same purpose.

> I tried this,
>
> #include <stdio.h>
> int main()
> {
> const int a = 32;
> int *p;
> p = &a;

That is a constraint violation. When assigning to a pointer object, "the 
type pointed to by the left has all the qualifiers of the type pointed 
to by the right" (6.5.16.1p1). The type that &a points at has a 
qualifier (const) that the type pointed at by p does not. A diagnostic 
message is mandatory. Producing an executable is optional. If an 
implementation should choose to produce such an executable, and if you 
should choose to try to execute it, the behavior is undefined.

The purpose of this constraint is to protect you from accidentally 
trying to change something that has been marked as something that should 
NOT be changed.
You can get cover up this problem (without actually removing it) with a 
cast: (int *)&a.

> *p = 45;

p points at a. a has been defined as 'const'. Even if you cover up the 
previous problem with a cast, this code still has undefined behavior:
"If an attempt is made to modify an object defined with a const 
qualified type through use of an lvalue with non-const-qualified type, 
the behavior is undefined." (6.7.3p6)

When the behavior is undefined, that means that the standard imposes no 
requirements on the behavior of the resulting program. In particular, it 
is allowed to produce exactly the behavior you incorrectly thought it 
was required to produce. It could also make your computer start playing 
"La Macarena".

> 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;
>    ^
> gamer@acid:~/practice_linux/linked_list$ ./a.out
> 45 0x7ffe6ce0f950
>
> So I get a warning and an address from .text section. Am I correct.

You're correct about getting a warning. As a block-scope variable 
associated with main(), 'a' is likely to have been allocated from the 
stack. However, since the behavior is undefined, in principle that 
address is completely meaningless.

> Also the const value got changed.

Since the behavior is undefined, that's one of the permitted possibilities

> #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.

This program has precisely the same problems as the previous one. 
However, since 'a' has been declared static, it could have been 
allocated in a different piece of memory. That's apparently a read-only 
block of memory, which would be reasonable for an object defined as 
'const', hence the seg-fault. This is one of the many possibilities 
allowed when the behavior of your code is undefined.

Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread


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