Re: gcc and initialization

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,
Ph. Marek wrote:
Hello everybody,

I have some structures in my program, where I could save some space by
overlapping them.

See this example:
	char *yes_no_auto[]= { "auto", "yes", "no" };
	char *yes_no[]= { "yes", "no" };
I know that gcc should fold identical strings in the binary; but if it's
not just strings, but entire structs, this won't help.

Now I wanted to use
	char *yes_no_auto[]= { "auto", "yes", "no" };
	char *yes_no[]=yes_no_auto+1;
but that doesn't work:
test.c:2: error: invalid initializer
I think this is a more basic issue rather than a gcc issue:

Initialization of non scalar values means to allocate a fixed size block of memory on the stack and fill it with values. This is done via the {}- operator .
Your first version fit into this paradigma, but the second one don't.
You have to write:

char *yes_no_auto[]= { "auto", "yes", "no" };
char *yes_no[]= {yes_no_auto[1], yes_no_auto[2]};

to point the entries of your non scalar fixed size array "yes_no" to the already defined entries in the array "yes_no_auto".

To fill a fixed size scalar value on the stack you have to write:

char *yes_no_auto[]= { "auto", "yes", "no" };
char **yes_no = yes_no_auto + 1;

but then the sizeof operator will only return the size of the scalar value itself.


Regards,
Steve

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux