A simple *Func question

Started by
3 comments, last by frizb 23 years, 10 months ago
Do function pointers need to be deleted? Still Learning...
Still Learning...
Advertisement
That depends.

If you store the function pointer in a static variable, i.e. one you declared in the beginning of your program, ex:
int theInt;
you don''t need to delete it. When it goes out of context, it will be automatically deallocated.

But if you store your function pointer in a dynamic variable, i.e. one you declare programmatically as a respond to some kind of action, you definitely need to deallocate it your self or your program will leak.

I''m not completely sure I''m acutally answering your question, then again I''m not completely sure what your question is :-)

Regards
toft
Just to be clear... say you have a function pointer:

int *myMonsterAI;

and at some point you set this to an existing function:

myMonsterAI = hairyMonsterAI;

then no, you don''t need to delete it. The function pointer is only using the space required for a pointer, and has not reserved any extra memory for the function.

Of course, it doesn''t hurt to set the function to NULL when you''re finished with it - for book-keeping if nothing else.
Just to be clear... say you have a function pointer:

int *myMonsterAI();

and at some point you set this to an existing function:

myMonsterAI = hairyMonsterAI;

then no, you don''t need to delete it. The function pointer is only using the space required for a pointer, and has not reserved any extra memory for the function.

Of course, it doesn''t hurt to set the function to NULL when you''re finished with it - for book-keeping if nothing else.
Function pointers to not need to be deleted because functions are not allocated memory.

And function pointers are declared:

int (*myMonsterAI)( args )

and not:

int* myMonsterAI();

The latter example returns an integer pointer.

Michael

This topic is closed to new replies.

Advertisement