Skip site navigation (1)Skip section navigation (2)
Date:      15 Jan 1998 21:03 EST
From:      "Andrew Atrens" <atrens@nortel.ca>
To:        joelh@gnu.org, hackers@FreeBSD.ORG
Subject:   Re: sharable static arrays?
Message-ID:  <199801160254.SAA14699@hub.freebsd.org>

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 :)

Andrew





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