Static array with #defined # of "extra" members

I need to create a static array of structures with the following
characteristics:

* The first few members of the array are pre-initialized
* The remaining members of the array are initialized to "zero"
* The number of additional members is determined by a #defined constant,
independently of the number of pre-initialized members

The struct all_foos will be at least large enough for 10
elements, possibly larger if there's (perverse) padding.

However, trying to use all_foos.builtin[5] might not work
(see "struct hack") and even if it works it is not necessarily
the case that &all_foos.builtin[4]+1 == &all_foos.extra[0]. You
need to use something like

#define foos ((struct foo*)&all_foos)
> * Is there any danger of the compiler inserting unwanted padding between
> the builtin and extra members of the anonymous struct?

In principle, yes. Padding can appear after any struct element,
even if the implementor's reasons for putting it there don't seem
convincing.
> * Is there a better way to do this?

struct foo foos[5 + NUM_EXTRA_FOOS] =
{ {1}, {2}, {3}, {4}, {5} };

The compiler might warn about the incomplete initializer, but C
defines the effect: The extras are initialized to zero-value. You
might be able to suppress the warning with a C99-style initializer:

It's undefined behavior, but it will work with most, if not all,
compilers.
> * Is there any danger of the compiler inserting unwanted padding between
> the builtin and extra members of the anonymous struct?

A compiler is allowed to add padding between any two structure members,
but I doubt that any would add padding here.
> * Is there a better way to do this?

All members beyond those for which there are explicit initializers are
guaranteed to be initialized to 0.

When I was first reading the question, I guessed that you would want the
compiler to count the number of initializers and then add NUM_EXTRA_FOOS
to that, but your example didn't show it, so I didn't try to answer that
question. (I'm not sure whether there's an answer; I think there might
be, but I suspect it would be ugly.)

Am 04.08.2012 04:05, schrieb Ian Pilcher:
> I need to create a static array of structures with the following
> characteristics:
>
> * The first few members of the array are pre-initialized
> * The remaining members of the array are initialized to "zero"
> * The number of additional members is determined by a #defined constant,
> independently of the number of pre-initialized members
> #define NUM_EXTRA_FOOS 5

Yes this can be done even without much preprocessor magic.

Defining your initializers in a macro

#define FIRST_ELEMS { 1 }, { 2 }, { 3 }, { 4 }, { 5 }

and then replace your struct definition by just an array where the size
expression is constructed from the sizeof of a compount literal.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!