// __CTOR_LIST__ symbol created by linker typedef void (*func_ptr) (void); extern func_ptr __CTOR_LIST__[]; extern func_ptr __DTOR_LIST__[]; //func_ptr __CTOR_LIST__[2] = {0, 0}; //func_ptr __DTOR_LIST__[2] = {0, 0}; int main() { unsigned int h; unsigned int nptrs; // Initialise the heap so that malloc and new will work // in constructors (then again you're not really supposed // to new in constructors) InitHeap((unsigned long *)HEAPADDR, (unsigned long)HEAPLEN); // on some systems, __CTOR_LIST__[0] contains the number // of pointers to global initialisation functions nptrs=(unsigned int)__CTOR_LIST__[0]; if (nptrs==(unsigned int)-1) { // on others, it is -1 and you see how many // pointers there are because the array is // null terminated nptrs=0; while (__CTOR_LIST__[nptrs+1]!=NULL) { nptrs++; } } // Call the initialisation functions in reverse order (?) for (h=nptrs; h>=1; h--) { __CTOR_LIST__[h](); } // // Do the rest of your code here!! // // Call Destructors func_ptr *p; for (p = __DTOR_LIST__ + 1; *p; p++) { (*p) (); } return (0); }