Date: Fri, 30 Jan 1998 16:53:25 -0500 (EST) From: Thomas David Rivers <rivers@dignus.com> To: atrens@nortel.ca, hackers@FreeBSD.ORG, joelh@gnu.org Subject: Re: sharable static arrays? Message-ID: <199801302153.QAA29966@lakes.dignus.com>
next in thread | raw e-mail | index | archive | help
> > > >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 -
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199801302153.QAA29966>