CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 33
  1. #1
    Join Date
    Oct 2007
    Posts
    8

    Good practice for knowing if something has been allocated?

    A common practice I employ within classes is to couple dynamically allocated arrays with a bool to keep track of whether the array is currently allocated. I'd like to know if there was a cleaner way of doing this? I can't find the existence of some kind of isalloc() function, but perhaps this could be done through exceptions (i.e. throws exception if you try and deallocate unallocated array)?

    Is there a best practice way of doing this?

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Good practice for knowing if something has been allocated?

    Set array pointer initially to NULL. When array is released, set pointer back to NULL. Condition ptr != NULL checks whether array is allocated.

  3. #3
    Join Date
    Oct 2007
    Posts
    8

    Re: Good practice for knowing if something has been allocated?

    Thankyou very much, that's the perfect solution. Cheers!

  4. #4
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Good practice for knowing if something has been allocated?

    These are macros that I always define if I'm in this situation too

    Code:
    #define DECLARE_ARRAY(type, name);\
       type * name = 0;
    
    #define DECLARE_ARRAY_WITH_SIZE(type, name, size);\
       type * name = new type[size];
    
    #define ARRAY_SIZE(type, name, size);\
       DELETE_ARRAY_NO_RESET(name);\
       name = new type[size];
    
    #define DELETE_ARRAY(name);\
       if (name){ delete[] name; name = 0;}
    
    #define DELETE_ARRAY_NO_RESET(name);\
       if (name) delete[] name;
    Last edited by ninja9578; December 13th, 2009 at 09:58 AM.

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Good practice for knowing if something has been allocated?

    Quote Originally Posted by Bogdanovist
    A common practice I employ within classes is to couple dynamically allocated arrays with a bool to keep track of whether the array is currently allocated. I'd like to know if there was a cleaner way of doing this? I can't find the existence of some kind of isalloc() function, but perhaps this could be done through exceptions (i.e. throws exception if you try and deallocate unallocated array)?
    Why not just use std::vector or some other appropriate container class, possibly of your own making if the standard containers do not suffice?

    Quote Originally Posted by ninja9578
    These are macros that I always define if I'm in this situation too
    You're funny

    (Although when you are not joking you do leave out the check for a null pointer and write your macros correctly, right? )
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Good practice for knowing if something has been allocated?

    Huh?

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Good practice for knowing if something has been allocated?

    Quote Originally Posted by ninja9578
    Huh?
    Oops, okay, I didn't realise that the syntax was correct: was the empty statement for each function-style macro part of the joke?

    EDIT:
    For the other point, I am talking about the fact that one would not normally write:
    Code:
    if (name){ delete[] name; name = 0;}
    rather, one would write:
    Code:
    delete[] name; name = 0;
    EDIT #2:
    Hate it spoil the joke if it really was one, but if it was not:
    I thought the macro thing was a joke because as far as I can tell, DECLARE_ARRAY, DECLARE_ARRAY_WITH_SIZE and DELETE_ARRAY_NO_RESET do not add any value to the code, and may even obfuscate it. DELETE_ARRAY does add value to the code, but it can easily be replaced by an inline function template:
    Code:
    template<typename T>
    inline void destroy_array(T*& p)
    {
        delete[] p;
        p = 0;
    }
    I am not entirely certain of the utility of ARRAY_SIZE, but again it is easily replaced by an inline function template:
    Code:
    template<typename T>
    inline void reassign_array(T*& p, std::size_t size)
    {
        delete[] p;
        p = new T[size];
    }
    Furthermore, the point seems moot when we can use std::vector.

    EDIT #3:
    Ah yes, I figured out why ARRAY_SIZE was bothering me: it is not exception safe. With a function template, we could write:
    Code:
    template<typename T>
    inline void reassign_array(T*& p, std::size_t size)
    {
        T* temp = new T[size];
        delete[] p;
        p = temp;
    }
    Doing something similiar with a function-style macro will likely require use of the do while loop trick to introduce scope.
    Last edited by laserlight; December 13th, 2009 at 02:03 PM.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Good practice for knowing if something has been allocated?

    I always wonder why folks in this day and age still want to work with raw arrays. Is it because the platform they are working under doesn't have equivalent stl collection libraries available? Is it because they aren't comfortable working with the templated libraries?

    Please let me understand.

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Good practice for knowing if something has been allocated?

    Quote Originally Posted by Alex F View Post
    Set array pointer initially to NULL. When array is released, set pointer back to NULL. Condition ptr != NULL checks whether array is allocated.
    Adding to this... If working with C++, get used to cleaning up any allocated memory in the destructor. If you get use to getting into the habit of writing the cleanup code in the destructor immediately after you've written the allocation code, you're more likely to end up with code that doesn't leak.

  10. #10
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Good practice for knowing if something has been allocated?

    Quote Originally Posted by Arjay
    Adding to this... If working with C++, get used to cleaning up any allocated memory in the destructor. If you get use to getting into the habit of writing the cleanup code in the destructor immediately after you've written the allocation code, you're more likely to end up with code that doesn't leak.
    Yes, and if you take my suggestion of using std::vector of a more appropriate container, then you would not even have to worry about that (unless you are writing your own container class from scratch).
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  11. #11
    Join Date
    Dec 2009
    Posts
    26

    Re: Good practice for knowing if something has been allocated?

    Have you considered coding your own garbage collector?

  12. #12
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Good practice for knowing if something has been allocated?

    Quote Originally Posted by laserlight View Post
    For the other point, I am talking about the fact that one would not normally write:
    Code:
    if (name){ delete[] name; name = 0;}
    rather, one would write:
    Code:
    delete[] name; name = 0;
    Oh, I see what you're talking about. The problem is that if you have a class like this:

    Code:
    class foo{
    public:
       foo(void){
          myArray = 0;  //may or may not ever actually get used
       }
       ~foo(void){
          delete[] myArray;  //Doesn't work, you have first check if it was initalized
       }
    private:
       int * myArray;
    };
    That's what the check was for in the macro.

  13. #13
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Good practice for knowing if something has been allocated?

    Quote Originally Posted by ninja9578
    That's what the check was for.
    No, that check would be useless. If the member pointer was not initialised, the check could still allow the delete[] to happen, leading to undefined behaviour. If the member pointer was initialised to be a null pointer or to point to a valid object (created by new[]), then the check is redundant.
    Last edited by laserlight; December 13th, 2009 at 03:08 PM.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  14. #14
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Good practice for knowing if something has been allocated?

    Quote Originally Posted by laserlight View Post
    Yes, and if you take my suggestion of using std::vector of a more appropriate container, then you would not even have to worry about that (unless you are writing your own container class from scratch).
    Agreed.

  15. #15
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Good practice for knowing if something has been allocated?

    Quote Originally Posted by ninja9578 View Post
    Oh, I see what you're talking about. The problem is that if you have a class like this:

    Code:
    class foo{
    public:
       foo(void){
          myArray = 0;  //may or may not ever actually get used
       }
       ~foo(void){
          delete[] myArray;  //Doesn't work, you have first check if it was initalized
       }
    private:
       int * myArray;
    };
    That's what the check was for in the macro.
    Isn't using delete on a NULL param a no-op so there isn't any reason to guard the delete statement?

Page 1 of 3 123 LastLast

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