Re: HEADS UP: getenv() and family API change

From: Erik Trulsson <ertr1013_at_student.uu.se>
Date: Wed, 11 Jul 2007 00:57:52 +0200
On Wed, Jul 11, 2007 at 02:46:19AM +0400, Andrey Chernov wrote:
> On Wed, Jul 11, 2007 at 07:36:02AM +1000, Peter Jeremy wrote:
> > On 2007-Jul-10 19:41:48 +0400, Andrey Chernov <ache_at_nagual.pp.ru> wrote:
> > >To say strictly, copying somewhere is not neccessary since this way works 
> > >too:
> > >
> > >static char *s = "PATH=/bin";
> > >
> > >putenv(s);
> > 
> > I thought the C compiler was still free to place the string into RO
> > memory and/or coalesce it with other strings in that case.
> > 
> > Wouldn't the following be clearer (s is forced to be writable):
> > 
> > static char s[] = "PATH=/bin";
> > 
> > putenv(s);
> 
> This two are the same, since there is no "const", so compiler can't put 
> static char *s
> into RO memory.

Not the pointer, but the string it points to can be put into read-only
memory.

Example:

static char *s = "PATH=/bin";
static char *t = "PATH=/bin";


Here both 's', and 't' can point into read-only memory where the string
"PATH=/bin" has been placed.  Not only that, they may point to the same
place, i.e. there need only be one copy of the string "PATH=/bin" in
the program (but there may be two distinct copies if the compiler does not
coalesce identical string constants.)
  

If on the other hand you use 

static char s[] = "PATH=/bin";
static char t[] = "PATH=/bin";


Then 's' and 't' are no longer pointers to a string constant, but arrays
that are initialized with the string "PATH=/bin".  These arrays are
modifiable and distinct - i.e. there will be (at least) two copies of the
string "PATH=/bin" in memory.




-- 
<Insert your favourite quote here.>
Erik Trulsson
ertr1013_at_student.uu.se
Received on Tue Jul 10 2007 - 20:58:03 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:39:14 UTC