Reputation: 6483
I want to use some evil macro magic to generate an array of pointers to all functions in a file. Unfortunately I'm limited to C99 and not very popular compiler.
I had an idea but I'm not sure if it is safe:
void foo(void)
{
;
}
void bar(void)
{
;
}
typedef void (*FuncPointer)(void);
FuncPointer array[2] = {foo, bar};
I tried it and it compiled and even worked but in C that doesn't actually meen that this kind of thing is safe or portable.
Are there any guarantees about initializaton of global arrays of pointers?
Upvotes: 0
Views: 1074
Reputation: 16056
Yes, this is guaranteed to work by the standard. All such arrays are filled in before main
is called.
Additionally, this is guaranteed to be finished before any global constructors are called, if you are using C++ or compiler-specific extensions that support global constructors in C (e.g. __attribute__((constructor))
in GNU C.)
The only case you might have to worry about is if you're writing your own kernel, in which case you have to take care of loading all parts of the executable.
Upvotes: 1