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


Groups > comp.lang.c > #107384

Re: Const values not stored in .text section

From jt@toerring.de (Jens Thoms Toerring)
Newsgroups comp.lang.c
Subject Re: Const values not stored in .text section
Date 2017-03-29 13:34 +0000
Organization Freie Universitaet Berlin
Message-ID <ek1reoFabkiU1@mid.uni-berlin.de> (permalink)
References <391e9782-09dd-43a6-a279-e9b72d6c2cd2@googlegroups.com>

Show all headers | View raw


acidlategamer@gmail.com wrote:
> 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.

Wrong assumption. First of all, all of this is an implementation
detail, nothing in the C standard requires the existence of a
.text segment (nor what we usually think of as a stack or heap).
Thus any observations you make on one machine, compiler and
operating system can't expected to necessarily also be valid
for a different machine, compiler or OS.

That out of the way (so the rest is a bit off-topic), compilers
often stick global and static variables into .data and .text (or
.rodata) segments, the former for modifiable data, the second
for const static and global variables (if the system supports
such a distinction)

Compilers typically don't put variables defined local to a
function (but not qualified as static) into the .data or
.text segment, but usually on the stack, const qualified
or not.

The result of this is the behaviour of your "experimental" pro-
grams, but the stack is writable, so no further consequemces.
In the first case 'a' is on the stack, the compiler warns you
about that. In the second case the compiler should warn you
about it too, 'a' is placed in the .text segment and a write
access results in a segmentation fault. Not untypical when you
do something you're supposed not to do - depending on circum-
stances it may seem to work anyway or something else happens.

The question of whether you can take the address of a variable
also has nothing to do with them being const. If you take the
address of a variable it most likely will prevent the compiler
from "optimizing out" the variable. If you wouldn't take the
address of 'a' in your programs the compiler might recognize
that the value can be used directly and thus it may simply drop
the definition, not using any memory for it, and replace any
use of 'a' by its value.

                         Regards, Jens
-- 
  \   Jens Thoms Toerring  ___      jt@toerring.de
   \__________________________      http://toerring.de

Back to comp.lang.c | Previous | Next — Previous in thread | Next 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