How do you check whether or not a pointer has been used with 'new' before calling delete[] on it?
Cheers.
Printable View
How do you check whether or not a pointer has been used with 'new' before calling delete[] on it?
Cheers.
Go to the line where it was new'd? ;)
Seriously, I don't think there is a way to programatically do that. "new" returns a pointer. Could be a pointer to one object, or many objects in a row.
Viggy
There is no way to check if a given memory location was "newed".... at least none that I know of. although you can check if a given memory location is "valid"/on the allocated heap - there are good ways and bad ways of checking for this....Quote:
Originally Posted by ggmn
the best way is to follow discipline while coding and always initialize pointers to NULL before allocation and after deallocation....
there are some platform specific apis you could use to check validity of a pointer, eg., IsBadPtr() for windows. if you are interested in these, try looking here:
http://msdn.microsoft.com/archive/de...tionmacros.asp
Fair comment, it's just there are a few functions that use the pointer and I thought there might be an MFC/win32 function that would query this, I'm quite suprised there isn't actually, but I suppose it's just the way the language works....Quote:
Go to the line where it was new'd?
Yep, I really should be doing that - I've seen it done enough times.....One day I'll go back to basics and learn programming properly, it's just so easy to start backwards with MFC......Quote:
the best way is to follow discipline while coding and always initialize pointers to NULL before allocation and after deallocation....
Cheers for your help.
The quick solution is whoever called new should call delete. That function knows exactly what to do with it. The other functions have no idea if the pointer passed to them was "newed" or not. Not only that, how do the other functions determine if "new" or "new[]" was used, or even malloc() or GlobalAlloc was used to dynamically allocate memory? What form of "delete" do you use? "delete"? "delete[]"? GlobalFree()? free()? No form of delete, because the pointer does not come from dynamically allocated memory?Quote:
Originally Posted by ggmn
Also, there is no OS function because "new/new[]" are pointers returned by the compiler's heap manager. The values of new/new[] are usually not pointers that the operating system has returned. The only pointers guaranteed to be "OS" pointers are ones returned from functions such as GlobalAlloc(), HeapAlloc(), i.e. the API heap allocation functions.
Regards,
Paul McKenzie
Actually, it's dirt easy. But it is VC++ only, and debugbuilds only.Quote:
Originally Posted by raghuvamshi
just call _CrtIsValidHeapPointer( )
However this also does not say if it was new of new[]. You can figure it out by checking the block type and memory allocation signature (again, VC++ and debug builds only) if it was allocated with new or new[].
There is no portable way of knowing if a pointer has been allocated, still points to the address where it was allocated and whether it was newed with new or new[]
Hi,
A method which I use is basically:
Hope this helps!Code:// Where the array is declared initialize to NULL or 0.
int *myArray = NULL;
// In another part of your program create the array like normal.
myArray = new int[255];
// Then when it comes to deleting your array:
if(myArray /*!= NULL*/)
{
delete[] myArray; // Free the entire array.
myArray = NULL; // Reset to NULL for later use.
}
Best Regards,
Lea Hayes
Not really necessary...you can call 'delete' safely with a 0 pointer.... :cool:Quote:
Originally Posted by lhayes00
Though the standard says that if you allocated with new [] and release with delete the behaviour is undefined, I think (not 100% sure) that MS compilers take care of that for those who wrongly use new/delete and replace delete with delete []. But you should not rely on that.
Using delete instead of delete[] (on VC++) does not make a difference as far as actually deallocating memory is concerned.Quote:
Originally Posted by cilu
However, delete will only call a destructor on the first element of the array, delete[] will destroy all objects.
With an array of a basic type like int, char, dword... this doesn't make a diffference since there isn't a destructor for these types.