Wednesday, 15 August 2012

c - Initializing global array of function pointers -



c - Initializing global array of function pointers -

i want utilize evil macro magic generate array of pointers functions in file. unfortunately i'm limited c99 , not popular compiler.

i had thought i'm not sure if safe:

void foo(void) { ; } void bar(void) { ; } typedef void (*funcpointer)(void); funcpointer array[2] = {foo, bar};

i tried , compiled , worked in c doesn't meen kind of thing safe or portable.

are there guarantees initializaton of global arrays of pointers?

yes, guaranteed work standard. such arrays filled in before main called.

additionally, guaranteed finished before global constructors called, if using c++ or compiler-specific extensions back upwards global constructors in c (e.g. __attribute__((constructor)) in gnu c.)

the case might have worry if you're writing own kernel, in case have take care of loading parts of executable.

c function-pointers array-initialization

No comments:

Post a Comment