-
January 26th, 2005, 05:31 PM
#1
delete[]
How do you check whether or not a pointer has been used with 'new' before calling delete[] on it?
Cheers.
-
January 26th, 2005, 05:41 PM
#2
Re: delete[]
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
-
January 26th, 2005, 05:49 PM
#3
Re: delete[]
Originally Posted by ggmn
How do you check whether or not a pointer has been used with 'new' before calling delete[] on it?
Cheers.
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....
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
-
January 26th, 2005, 05:58 PM
#4
Re: delete[]
Go to the line where it was new'd?
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....
the best way is to follow discipline while coding and always initialize pointers to NULL before allocation and after deallocation....
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......
Cheers for your help.
-
January 26th, 2005, 06:21 PM
#5
Re: delete[]
Originally Posted by ggmn
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....
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?
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
-
January 26th, 2005, 06:50 PM
#6
Re: delete[]
Originally Posted by raghuvamshi
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....
Actually, it's dirt easy. But it is VC++ only, and debugbuilds only.
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[]
-
January 26th, 2005, 06:57 PM
#7
Re: delete[]
Hi,
A method which I use is basically:
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.
}
Hope this helps!
Best Regards,
Lea Hayes
-
January 27th, 2005, 04:14 AM
#8
Re: delete[]
Originally Posted by lhayes00
Code:
// Then when it comes to deleting your array:
if(myArray /*!= NULL*/)
Not really necessary...you can call 'delete' safely with a 0 pointer....
-
January 27th, 2005, 04:25 AM
#9
Re: delete[]
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.
-
January 27th, 2005, 01:42 PM
#10
Re: delete[]
Originally Posted by cilu
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.
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|