CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Join Date
    Oct 2007
    Posts
    41

    Avoiding Dynamic Allocation

    How can I write this same code without the use of dynamic allocation?

    Code:
    class AClass {
    BClass *ptr;
    public:
    AClass() : ptr(new BClass (10)) {}
    };
    Thanx,

    John

  2. #2
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: Avoiding Dynamic Allocation

    If there are no circular dependencies you can simply write

    Code:
    class AClass
    {
       BClass BC[10];
    };
    - Guido

  3. #3
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: Avoiding Dynamic Allocation

    Quote Originally Posted by GNiewerth
    If there are no circular dependencies you can simply write

    Code:
    class AClass
    {
       BClass BC[10];
    };
    I think you've misread the OP's post, since 10 is the value to initialise the BClass with, not that 10 of them are needed.

    You could do the following:
    Code:
    class AClass {
    BClass bClass;
    public:
    AClass() : bClass(10) {}
    };

  4. #4
    Join Date
    Oct 2007
    Posts
    41

    Re: Avoiding Dynamic Allocation

    Thanks for the response.

    I know that code written this way is prone to memory leak. How can I remove memory leaks from my programs and also avoid repeated deletion?

    Thanx,

    John

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

    Re: Avoiding Dynamic Allocation

    I know that code written this way is prone to memory leak. How can I remove memory leaks from my programs and also avoid repeated deletion?
    If you took the suggestion to store an object instead of a pointer as the member then you do not have to worry about a memory leak.
    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
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: Avoiding Dynamic Allocation

    In the original code:
    Code:
    class AClass 
    {
    BClass *ptr;
    public:
    AClass() : ptr(new BClass (10)) {}
    };
    If there is requirement that you must have pointer to a class-type, you need not to initialize in initializer's list (like above). You can safely do:
    Code:
    AClass() 
    {
    ptr = new BClass (10);
    }
    Which avoids code clutter. Initializer list is mandatory for ONLY following cases:
    1. Base class is having non-default constructor.
    2. You class is having data member of class-type, which does not have default constructor.
    3. For const-data members (basic or class type)
    4. For refernce type (X &) - not pointer types.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Avoiding Dynamic Allocation

    Initializer lists should be preferred for most things. However, I'll agree that it's probably a good idea to keep new's out of them.

    To avoid a memory leak if you *do* use new in a constructor, use delete in the corresponding destructor.

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

    Re: Avoiding Dynamic Allocation

    If there is requirement that you must have pointer to a class-type, you need not to initialize in initializer's list (like above). You can safely do:
    Yes, I think the important point is that it is safer not to use new in the initialisation list: GotW #66: Constructor Failures. In particular:
    Moral #3: Always perform unmanaged resource acquisition in the constructor body, never in initializer lists. In other words, either use "resource acquisition is initialization" (thereby avoiding unmanaged resources entirely) or else perform the resource acquisition in the constructor body.

    Moral #4: Always clean up unmanaged resource acquisition in local try-block handlers within the constructor or destructor body, never in constructor or destructor function-try-block handlers.
    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

  9. #9
    Join Date
    Oct 2007
    Posts
    41

    Re: Avoiding Dynamic Allocation

    Thanks for the replies.

    So basically memory leak should be handled by the destructor, using delete etc. What about repeated deletion?

    Thanx,

    John

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

    Re: Avoiding Dynamic Allocation

    What about repeated deletion?
    Set pointers to NULL after using delete on them (unless the pointer goes out of scope immediately after that). Of course, this will not help if you use delete on another pointer to the same object.
    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
    May 2003
    Location
    San Antonio TX
    Posts
    380

    Re: Avoiding Dynamic Allocation

    Quote Originally Posted by JohnSmith70
    Thanks for the replies.

    So basically memory leak should be handled by the destructor, using delete etc. What about repeated deletion?

    Thanx,

    John
    repeated delete can be avoided by checking the pointer before attempting to delete it.

    (same as what laserlight said, with example)
    i.e.

    Code:
    if(ptr)
    {
        delete ptr;
        ptr = 0;
    }

    1. Check the pointer - only attempt to delete if !NULL
    2. delete pointer
    3. set pointer to NULL
    John 3:16
    For God so loved the world ...

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

    Re: Avoiding Dynamic Allocation

    1. Check the pointer - only attempt to delete if !NULL
    Not necessary. Deleting a null pointer is perfectly okay.
    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

  13. #13
    Join Date
    May 2003
    Location
    San Antonio TX
    Posts
    380

    Re: Avoiding Dynamic Allocation

    Quote Originally Posted by laserlight
    Not necessary. Deleting a null pointer is perfectly okay.
    I did not know that.

    Thanks.
    John 3:16
    For God so loved the world ...

  14. #14
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Avoiding Dynamic Allocation

    More importantly in this case, you'll have to define the operator= and copy constructor in such a way that no two objects of a class share the same pointer. Typically this means allocate a new dynamic object in both cases; and in the case of operator=, also first delete the existing object (if any).

    If your operator= and copy constructor are properly defined, then it will be impossible for a destructor call to result in multiple deletion because there will be only one object holding each dynamically allocated memory block.

  15. #15
    Join Date
    Aug 2005
    Location
    LI, NY
    Posts
    576

    Re: Avoiding Dynamic Allocation

    Here's the flavor of double deletion that a beginner is most likely to encounter:
    Code:
    class Foo
    {
    public:
        Foo()
        {
            m_ptr = new int;
        }
        ~Foo()
        {
            delete m_ptr;
        }
    private:
        int * m_ptr;
    };
    
    int main()
    {
        Foo a;
        Foo b(a);
    }
    Since no copy constructor is provided, both a and b will point to the same memory, and both will try to delete that memory when they fall out of scope. This problem is avoided by writing a proper copy constructor and assignment operator.

    EDIT: Lindley beat me to it.
    - Alon

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