Wiki: C PlusPlus - Static Objects
Posted: Tue Jan 13, 2009 11:21 am
Hi,
may be it is a simple question, but it was coming up, when I read the in the title mentioned article.
The text says that to enable global objects, I need "int __cxa_atexit(void (* f)(void *), void *p, void *d);", which is right, because without the code it won't compile. There is the following Code in the Article, to implement this:
The Code works fine and I understood how it works, but why is object an Array with 32 elements?
Is it, that it looks a bit prettier or why?
Cheers Christian
may be it is a simple question, but it was coming up, when I read the in the title mentioned article.
The text says that to enable global objects, I need "int __cxa_atexit(void (* f)(void *), void *p, void *d);", which is right, because without the code it won't compile. There is the following Code in the Article, to implement this:
Code: Select all
extern "C"
{
int __cxa_atexit(void (*f)(void *), void *p, void *d);
void __cxa_finalize(void *d);
};
void *__dso_handle; /*only the address of this symbol is taken by gcc*/
struct object
{
void (*f)(void*);
void *p;
void *d;
} object[32] = {0};
unsigned int iObject = 0;
int __cxa_atexit(void (*f)(void *), void *p, void *d)
{
if (iObject >= 32) return -1;
object[iObject].f = f;
object[iObject].p = p;
object[iObject].d = d;
++iObject;
return 0;
}
/* This currently destroys all objects */
void __cxa_finalize(void *d)
{
unsigned int i = iObject;
for (; i > 0; --i)
{
--iObject;
object[iObject].f(object[iObject].p);
}
}
Is it, that it looks a bit prettier or why?
Cheers Christian