CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Jul 2005
    Posts
    1,030

    A coding style question

    Suppose I have a function in which there is multiple return statements and also I allocate a memory in heap at the beginning of function, so I need to deallocate the memory in every return statement so as not to leak the memory in every situation. Not only there is redundant delete statements but also it is error prone. For example, what if I forget to call the delete before one of return statements? I wander if there is any elegant solution to approach this issue? Thanks.

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

    Re: A coding style question

    An elegant solution is to use a smart pointer or container, as appropriate.
    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

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

    Re: A coding style question

    Quote Originally Posted by LarryChen View Post
    Suppose I have a function in which there is multiple return statements and also I allocate a memory in heap at the beginning of function, so I need to deallocate the memory in every return statement so as not to leak the memory in every situation. Not only there is redundant delete statements but also it is error prone. For example, what if I forget to call the delete before one of return statements? I wander if there is any elegant solution to approach this issue? Thanks.
    Yes, the solution is to use classes that automatically clean up on destruction, such as vector, and not just allocate raw memory.

    In other words, RAII.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Jul 2005
    Posts
    1,030

    Re: A coding style question

    Ok, here is an approach using RAII. But since I allocate an array of memory so I don't know how to use smart pointer to do the similar thing. Does smart pointer work with array of memory?
    Code:
    class B
    {
    public:
    	B(int size)
    	{
    		data = new int[size];
    	}
    
    	~B()
    	{
    		delete[] data;
    	}
    
    	int& operator[](int index)
    	{
    		return data[index];
    	}
    
    private:
    	int* data;
    };
    
    bool UseArray(int a[], int size)
    {	
    	//int* b = new int[size];
    	B b(size);
    
                    if(...)
                    {
                        //delete[] b;
                        return true;
                    }
    
    
                    ...
    
    
                    if(...)
                    {
                          // delete[] b;
                          return true;
                    }
    
    	//delete[] b;
    	return false;
    }
    Any comments are welcome.

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

    Re: A coding style question

    Quote Originally Posted by LarryChen
    Ok, here is an approach using RAII.
    Why did you not reach for std::vector instead of rolling your own solution?

    Quote Originally Posted by LarryChen
    But since I allocate an array of memory so I don't know how to use smart pointer to do the similar thing. Does smart pointer work with array of memory?
    It depends on the smart pointer and how you use it, but generally a container would be a more appropriate solution.
    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
    Jul 2005
    Posts
    1,030

    Re: A coding style question

    I agree here stl container is more appropriate. But if I want to use auto_ptr, how can I allocate an array of memory by using it? Thanks.
    Quote Originally Posted by laserlight View Post
    Why did you not reach for std::vector instead of rolling your own solution?


    It depends on the smart pointer and how you use it, but generally a container would be a more appropriate solution.

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

    Re: A coding style question

    Quote Originally Posted by LarryChen
    But if I want to use auto_ptr, how can I allocate an array of memory by using it?
    You cannot, at least not without invoking undefined behaviour when it comes to destroying the array.
    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
    Join Date
    Jul 2005
    Posts
    1,030

    Re: A coding style question

    So auto_ptr doesn't work with array of memory?
    Quote Originally Posted by laserlight View Post
    You cannot, at least not without invoking undefined behaviour when it comes to destroying the array.

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

    Re: A coding style question

    Quote Originally Posted by LarryChen
    So auto_ptr doesn't work with array of memory?
    Yes. If you really insist, use something like boost::scoped_array, or a smart pointer with a custom deleter.
    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

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: A coding style question

    Quote Originally Posted by LarryChen View Post
    I wander if there is any elegant solution to approach this issue? Thanks.
    Since you keep using it, I feel I need to point out the correct word here is "wonder".

  11. #11
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: A coding style question

    Quote Originally Posted by LarryChen View Post
    So auto_ptr doesn't work with array of memory?
    nope.

    Look into boost::scoped_array, or (boost/tr1/std)::shared_pointer can handle arrays. At least, the source code says so, but I'm not 100% sure what the standard says.

    Worst case scenario, shared_pointer can take a "deleter" argument, giving it the power to destroy/release anything, regardless of method of destruction.

    Or, use a container, they are RAII too. And they allocate on the heap too, so you have no reason not to use one.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  12. #12
    Join Date
    Jul 2005
    Posts
    1,030

    Re: A coding style question

    How'd I implement a custom deleter for smart pointer? I searched on the web but couldn't find any good resource on that matter. Thanks.
    Quote Originally Posted by laserlight View Post
    Yes. If you really insist, use something like boost::scoped_array, or a smart pointer with a custom deleter.

  13. #13
    Join Date
    Jul 2005
    Posts
    1,030

    Re: A coding style question

    Thanks for pointing it out.
    Quote Originally Posted by GCDEF View Post
    Since you keep using it, I feel I need to point out the correct word here is "wonder".

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

    Re: A coding style question

    Quote Originally Posted by LarryChen
    I searched on the web but couldn't find any good resource on that matter.
    hmm... I do not really like using this, but in this case I find your claim very difficult to believe since it was trivial for me to find a reasonably good resource on this: custom deleter.
    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

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

    Re: A coding style question

    Here's a class I use occasionally.

    Code:
    enum deallocator {
        FREE,
        DELETE,
        ARRAY
    };
    
    #define AUTODELETE(type, name, value, destruc)\
        type * name = value; AutoDelete<type> autoclean##name(name, destruc);
    
    template <typename T>
    class AutoDelete {
    public:
        AutoDelete(T * ptr, deallocator d){
    	   myptr = ptr;
    	   mydestroy = d;
        }
        ~AutoDelete(void){
    	   switch (mydestroy){
    		  case FREE:
    			 free(myptr);
    			 break;
    		  case DELETE:
    			 delete myptr;
    			 break;
    		  case ARRAY:
    			 delete[] myptr;
    			 break;
    	   }
        }
    private:
        T * myptr;
        deallocator mydestroy;
    };
    You can use it like this:

    Code:
    void foo(void){
        AUTODELETE(int, array, new int[50], ARRAY);
        cout << array[5] << endl;
        AUTODELETE(int, array2, (int*)malloc(50 * sizeof(int)), FREE);
    }
    Of course, it's good to specialize different wrappers for different types of destructors.

    If you'd like I have a complete library of automatic wrappers if you'd like me to post them.
    Last edited by ninja9578; July 24th, 2010 at 03:37 PM.

Page 1 of 2 12 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