CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: delete[]

  1. #1
    Join Date
    Jul 2004
    Posts
    243

    delete[]

    How do you check whether or not a pointer has been used with 'new' before calling delete[] on it?

    Cheers.

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    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

  3. #3
    Join Date
    Nov 2004
    Location
    Virginia, The lovers' state
    Posts
    64

    Re: delete[]

    Quote 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

  4. #4
    Join Date
    Jul 2004
    Posts
    243

    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.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: delete[]

    Quote 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

  6. #6
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: delete[]

    Quote 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[]

  7. #7
    Join Date
    Jan 2002
    Location
    United Kingdom
    Posts
    491

    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

  8. #8
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: delete[]

    Quote 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....

  9. #9
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  10. #10
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: delete[]

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured