I've seen that it is valid to deallocate any variable using delete, such as:
# int numVar = 5; delete numVar;
and numVar no longer exists (which is the same as letting a variable go out of scope).
I'm wanting to do some lazy de-allocation where I can delete a variable located at a pointer which is sometimes allocated by new, but not always (right before my program closes, so no chance of needing that variable anyways).
Rather than keep track of each individual variable if it has been allocated dynamically, I am curious if this works:
Would it be valid within C++ to do this? I am concerned with this being valid based on standards more than if it happens to work on one or two compilers...
# int numVar = 5; delete numVar;
and numVar no longer exists (which is the same as letting a variable go out of scope).
I'm wanting to do some lazy de-allocation where I can delete a variable located at a pointer which is sometimes allocated by new, but not always (right before my program closes, so no chance of needing that variable anyways).
Rather than keep track of each individual variable if it has been allocated dynamically, I am curious if this works:
Code:
#include <math.h>
int *numVarPtr, numVar = 5;
if (rand() % 2) {
numVarPtr = new int;
*numVarPtr = 6;
} else {
numVarPtr = &numVar;
} // End if
delete *numVarPtr;
Comment