From owner-freebsd-current@FreeBSD.ORG Wed Jul 11 00:05:15 2007 Return-Path: X-Original-To: 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 D4E3016A400 for ; Wed, 11 Jul 2007 00:05:15 +0000 (UTC) (envelope-from youshi10@u.washington.edu) Received: from mxout5.cac.washington.edu (mxout5.cac.washington.edu [140.142.32.135]) by mx1.freebsd.org (Postfix) with ESMTP id A6D5713C455 for ; Wed, 11 Jul 2007 00:05:14 +0000 (UTC) (envelope-from youshi10@u.washington.edu) Received: from hymn07.u.washington.edu (hymn07.u.washington.edu [140.142.8.53]) by mxout5.cac.washington.edu (8.13.7+UW06.06/8.13.7+UW07.06) with ESMTP id l6B05EBJ013404 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Tue, 10 Jul 2007 17:05:14 -0700 Received: from localhost (localhost [127.0.0.1]) by hymn07.u.washington.edu (8.13.7+UW06.06/8.13.7+UW07.03) with ESMTP id l6B05DXI005820 for ; Tue, 10 Jul 2007 17:05:13 -0700 X-Auth-Received: from [192.55.52.3] by hymn07.u.washington.edu via HTTP; Tue, 10 Jul 2007 17:05:13 PDT Date: Tue, 10 Jul 2007 17:05:13 -0700 (PDT) From: youshi10@u.washington.edu To: current@freebsd.org In-Reply-To: <20070710225752.GA3000@owl.midgard.homeip.net> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-PMX-Version: 5.3.2.304607, Antispam-Engine: 2.5.1.298604, Antispam-Data: 2007.7.10.164834 X-Uwash-Spam: Gauge=IIIIIII, Probability=7%, Report='NO_REAL_NAME 0, __CT 0, __CT_TEXT_PLAIN 0, __HAS_MSGID 0, __MIME_TEXT_ONLY 0, __MIME_VERSION 0, __SANE_MSGID 0' 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: Wed, 11 Jul 2007 00:05:15 -0000 On Wed, 11 Jul 2007, Erik Trulsson wrote: > 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 I'm confused what you're referring to as RO memory -- I thought that only const applied in this case: #include int main () { static char *s = "PATH=/bin"; s = "PATH=/sbin"; printf("%s\n", s); return 0; } filc9175[409]% gcc -o try try.c filc9175[410]% ./try PATH=/sbin Doesn't static (in terms of variables) only state that the memory address and values are not to be released to the heap again after the function scope exits? -Garrett