[PATCH] convert local constant arrays to static

Bernd Petrovitsch bernd at firmix.at
Wed Feb 22 17:25:12 UTC 2006


On Wed, 2006-02-22 at 10:52 -0500, Rob Landley wrote:
> On Wednesday 22 February 2006 4:30 am, Bernd Petrovitsch wrote:
> > On Wed, 2006-02-22 at 09:00 +0200, Denis Vlasenko wrote:
> > [...]
> >
> > > Patch basically does this:
> >
> > Nice - "const"ing is always a good thing.
> >
> > > -               char *extn[] = {"", ".zip", ".ZIP"};
> > > +               static const char *const extn[] = {"", ".zip", ".ZIP"};
> >
> > <anal>
> > Probably in this one case
> > ----  snip  ----
> > static const char extn[5] = {"", ".zip", ".ZIP"};
> > ----  snip  ----
> > saves a few bytes since there are no 4-byte pointers in between.
> > </anal>
> 
> I don't understand that, could you explain?

First above should have read (sorry for the confusion coming out of my
head)
----  snip  ----
static const char extn[5][] = {"", ".zip", ".ZIP"};
----  snip  ----
This gives a 3 element array with 5 chars in each element totalling to 3
* 5 = 15 bytes:
----  snip  ----
static const char * const extn[] = {"", ".zip", ".ZIP"};
----  snip  ----
gives a 3 element array of "char *" (bytes each) and these 3 pointers
point to the three given strings. Assuming no overlapping in these
strings this totals to 3 * 4 (the arry of pointers) + 1 + 5 + 5 (the
lengths of the 3 strings) = 23 bytes.
In both I assumed alignment of 1 byte for chars and 32bit pointers (with
64bit pointers the second value would be higher). And I let the "const"
in for similarity to the originally posted sources ("const" should make
no difference size-wise).
At least the quoted source use the pointers/arrays "naturally" and
should be no problem.
For those who don't believe, just printout the addresses of the various
elements and compare them.

	Bernd
-- 
Firmix Software GmbH                   http://www.firmix.at/
mobil: +43 664 4416156                 fax: +43 1 7890849-55
          Embedded Linux Development and Services




More information about the busybox mailing list