Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 11 Jul 2007 06:58:21 +0400
From:      Andrey Chernov <ache@nagual.pp.ru>
To:        Peter Jeremy <peterjeremy@optushome.com.au>, freebsd-current@freebsd.org
Subject:   Re: HEADS UP:  getenv() and family API change
Message-ID:  <20070711025820.GA35503@nagual.pp.ru>
In-Reply-To: <20070710225752.GA3000@owl.midgard.homeip.net>
References:  <20070703182400.Q1449@baba.farley.org> <Xuu8UV3Bay@dmeyer.dinoex.sub.org> <20070709145418.T52164@thor.farley.org> <20070710154148.GA22873@nagual.pp.ru> <20070710213602.GX3434@turion.vk2pj.dyndns.org> <20070710224619.GA31654@nagual.pp.ru> <20070710225752.GA3000@owl.midgard.homeip.net>

next in thread | previous in thread | raw e-mail | index | archive | help
On Wed, Jul 11, 2007 at 12:57:52AM +0200, Erik Trulsson wrote:
> 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.

Yes, I agree. Moreover, currently gcc 4.2.0 coalesce "char *" pointed to 
identical string constants and move them to .rodata, so s[] way is better.

-- 
http://ache.pp.ru/



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20070711025820.GA35503>