From owner-freebsd-hackers Fri Jan 30 14:12:24 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id OAA05060 for hackers-outgoing; Fri, 30 Jan 1998 14:12:24 -0800 (PST) (envelope-from owner-freebsd-hackers@FreeBSD.ORG) Received: from elvis.vnet.net (elvis.vnet.net [166.82.1.5]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id OAA04939 for ; Fri, 30 Jan 1998 14:12:03 -0800 (PST) (envelope-from rivers@dignus.com) Received: from ponds.dignus.com (ponds.vnet.net [166.82.177.48]) by elvis.vnet.net (8.8.8/8.8.4) with ESMTP id RAA02844; Fri, 30 Jan 1998 17:12:04 -0500 (EST) Received: from lakes.dignus.com (lakes [10.0.0.3]) by ponds.dignus.com (8.8.5/8.8.5) with ESMTP id RAA00928; Fri, 30 Jan 1998 17:10:21 -0500 (EST) Received: (from rivers@localhost) by lakes.dignus.com (8.8.7/8.6.9) id QAA29966; Fri, 30 Jan 1998 16:53:25 -0500 (EST) Date: Fri, 30 Jan 1998 16:53:25 -0500 (EST) From: Thomas David Rivers Message-Id: <199801302153.QAA29966@lakes.dignus.com> To: atrens@nortel.ca, hackers@FreeBSD.ORG, joelh@gnu.org Subject: Re: sharable static arrays? Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG X-To-Unsubscribe: mail to majordomo@FreeBSD.org "unsubscribe hackers" > > > >Hmmmm.... Either way, it's illegal to define 'foobared' twice. > > Sorry, bad example. > > >If these were auto variables, then strategy a would require both stack > >space for the array at runtime, and text space for the initial value > >in the executable, whereas strategy b should only require one copy of > >its space in either text or data, I still haven't determined which. > > The initializer on the right of the `=' is *always* a const. and gets > stored in the text segment. Fortunately, if you make the thing on the > left of the `=' a const most compilers will not duplicate the store. > > Therefore, > > const char foob[10] = "0123456789"; > > will use 10 bytes ( all in text ) , and > > char fooba[10] = "0123456789"; > > will use 20 bytes ( ten in text, ten in data ). It's that simple. ;) > Now, what's interesting is that > > char foobar[10] = " "; > > will *also* use 20 bytes - the compiler will pad the initializer with > zeroes. > > So if you have a big dataset, make it const. You'll only get one copy (which > is all you really want) and it'll be in a shareable text segment :) Yes - this is a good explanation... Just to add to the one example, char foobar[10] = " "; doesn't necessarily have to consume 20 bytes in the object file. Many systems "fill in" at module startup.. so you may have simply two bytes for the string, and a descriptor of some kind which indicates the rest of the array should be filled with zeros. > > Andrew > > - Dave Rivers -