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

Thread: How to determine if a buffer is freed or not

  1. #1
    Join Date
    Nov 2010
    Posts
    54

    How to determine if a buffer is freed or not

    I use new to allocate a buffer, as follows:

    BYTE *p;

    p = new BYTE[20];


    ...

    delete p;


    After p is deleted, if I do NOT assign NULL to p, is there a way to determine whether it has already been freed?

    Thanks

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,759

    Re: How to determine if a buffer is freed or not

    Yes, but not in your code. It is how DEBUG_NEW works.
    In your code you have to assign NULL to p after deleting it:
    Code:
    delete p;
    p = NULL;
    Victor Nijegorodov

  3. #3
    Join Date
    Jan 2012
    Location
    India
    Posts
    193

    Re: How to determine if a buffer is freed or not

    1. With delete instruction , write p = NULL; Then u can easily check.

    2. Increment a flag variable with new and Decrement the same with delete.
    When it is 0 , assign the size ...

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,190

    Re: How to determine if a buffer is freed or not

    Code:
    BYTE *p;
    p = new BYTE[20];
    ...
    delete p;
    as new is allocating memory for an array, then the array version of delete should be used
    Code:
    delete [] p;

    Instead of having new/delete in your code - which can be prone to errors, consider using RAII construct or unique_pointer/shared_pointer.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.7.0)

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

    Re: How to determine if a buffer is freed or not

    Quote Originally Posted by 2kaud View Post
    Instead of having new/delete in your code - which can be prone to errors, consider using RAII construct or unique_pointer/shared_pointer.
    While unique_ptr will work since it has support for array-delete. it's worth mentioning that shared_ptr does not and should thus not be used for arrays. There was talks about adding this to C++14 but I haven't managed to keep entirely up to date on that. It's on my list of things to do in 2015.

    For databuffers however, it's typically easier to use vector instead of unique_ptr.

    unique_ptr with array-delete does have it's uses when you're getting the raw array pointer returned from some "older" bit of code where you don't have so much control over or which for various reasons can't (aren't allowed to) be changed to use a vector. I would call it a last resort when for whatever reason vector is out. (I do mean the "with array-delete" specific here, there is definately a use for unique_ptr on single objects).

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)