deleting dynamically allocated variables outside their declaring functon

Started by
14 comments, last by Iron Eye 20 years, 9 months ago


To read a declaration, read from the variable name, first to the right, then to the left, and make parentheses matter. Thus:

void * func(); // func is a function returning pointer to voidvoid (*func)(); // func is a pointer to a function returning voidint & * ptr; // ptr is a pointer to a reference to an int, which is illegalint * & ptr; // ptr is a reference to a pointer to an int, which is fineinline void DeletePointer( int * & ptr ){  delete ptr;  ptr = 0;} 


Or, more flexibly:

template< class T >void DeletePointer( T * & ptr ){  delete ptr;  ptr = 0;} 


Of course, if you allocate with new[], you have to delete with delete[].
Advertisement
To answer the OP’s question, you can delete dynamically created variables anywhere in your code where the pointer is visible. But you should know the scope of your dynamically created objects and it is good practice to create and delete things in the context of that scope.

For example if you create an array of integers at the beginning of your main function you should be deleting them at the end of main. If you are using new in the constructor of an object, you should be deleting them in the destructor.

It’s more of a programming preference/style thing than something you have to do, but your code will probably be more readable if you follow such a convention.

PS. Iron Eye, you will have memory leak in the second piece of code you posted.



[edited by - KiwiMelon on July 6, 2003 8:29:18 PM]
quote:Original post by ToohrVyk
Something like :

#define DELETE( X ) { if( X != NULL ) { delete X; X = NULL; } }
The comparison to NULL is redundant, as delete-ing a null pointer is guaranteed to do nothing.
quote:Original post by IvanTheTerrible
And there''s no need to set the pointer to zero afterwards as if you''re going to reuse it, the call to new will overwrite its contents anyway.

HTH


Setting the deleted pointer to zero is a very good practice, IMO. It makes it impossible for your program to try to access that memory later (which would lead to a pretty hard-to-find bug). It''s pretty common practice to do so.

Iron Eye, the code you posted will work fine, but most people just use a macro for this thing, like:
#define DELETE( X ) { delete X; X = NULL; }
quote:Original post by andy_fish
[Setting the deleted pointer to zero] makes it impossible for your program to try to access that memory later...
...so does letting the pointer go out of scope.
Beer hunter: sorry, I got too much used to smart pointers, with reference count and such... where you need to do :

if( P != NULL ) { P->Release( ); P = NULL; }

Besides, adding a "if( P != NULL )" condition each time you access a pointer makes it indeed impossible for the program to use a pointer that was deleted, whereas you can''t if you don''t set the pointer to NULL.

ToohrVyk

This topic is closed to new replies.

Advertisement