From owner-freebsd-current@FreeBSD.ORG Tue Jul 10 22:58:03 2007 Return-Path: X-Original-To: freebsd-current@freebsd.org Delivered-To: freebsd-current@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 6509F16A41F for ; Tue, 10 Jul 2007 22:58:03 +0000 (UTC) (envelope-from erikt@midgard.homeip.net) Received: from ch-smtp02.sth.basefarm.net (ch-smtp02.sth.basefarm.net [80.76.149.213]) by mx1.freebsd.org (Postfix) with ESMTP id E474B13C457 for ; Tue, 10 Jul 2007 22:58:02 +0000 (UTC) (envelope-from erikt@midgard.homeip.net) Received: from c83-253-10-135.bredband.comhem.se ([83.253.10.135]:55323 helo=falcon.midgard.homeip.net) by ch-smtp02.sth.basefarm.net with smtp (Exim 4.66) (envelope-from ) id 1I8OeZ-0003WP-9b for freebsd-current@freebsd.org; Wed, 11 Jul 2007 00:58:02 +0200 Received: (qmail 98378 invoked from network); 11 Jul 2007 00:57:52 +0200 Received: from owl.midgard.homeip.net (10.1.5.7) by falcon.midgard.homeip.net with SMTP; 11 Jul 2007 00:57:52 +0200 Received: (qmail 3059 invoked by uid 1001); 11 Jul 2007 00:57:52 +0200 Date: Wed, 11 Jul 2007 00:57:52 +0200 From: Erik Trulsson To: Andrey Chernov , Peter Jeremy , freebsd-current@freebsd.org Message-ID: <20070710225752.GA3000@owl.midgard.homeip.net> Mail-Followup-To: Andrey Chernov , Peter Jeremy , freebsd-current@freebsd.org References: <20070703182400.Q1449@baba.farley.org> <20070709145418.T52164@thor.farley.org> <20070710154148.GA22873@nagual.pp.ru> <20070710213602.GX3434@turion.vk2pj.dyndns.org> <20070710224619.GA31654@nagual.pp.ru> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20070710224619.GA31654@nagual.pp.ru> User-Agent: Mutt/1.5.14 (2007-02-12) X-Originating-IP: 83.253.10.135 X-ACL-Warn: Too high rate of unknown addresses received from you X-Scan-Result: No virus found in message 1I8OeZ-0003WP-9b. X-Scan-Signature: ch-smtp02.sth.basefarm.net 1I8OeZ-0003WP-9b d02cd40d6c494ee38f81f265ce8cab90 Cc: Subject: Re: HEADS UP: getenv() and family API change X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 10 Jul 2007 22:58:03 -0000 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 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. -- Erik Trulsson ertr1013@student.uu.se