Why it is not good code for constructor

This is a discussion on Why it is not good code for constructor within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, Here is a sample from Dr. Dobb C++. In the analysis, the code is bad below. But I ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    Why it is not good code for constructor

    Hello everyone,


    Here is a sample from Dr. Dobb C++. In the analysis, the code is bad below.

    But I do not think the code is bad,

    1. if bad_alloc is thrown in new int[], we just catch it and write some log;
    2. if there are any exception in B's constructor, we will also be in catch block and we could also write some log.

    Why it is bad code? Any comments?

    (I do not agree that there is resource leak, since if we met with bad_alloc in new int[], there is no memory allocated at all, so no root of memory/resource leak).


    http://www.ddj.com/cpp/184401297

    Code:
    C::C(int)
    try
       : B(new int[n]) // horrible!
    {
       ...
    }
    catch(Error &e)
    {
       
    }


    thanks in advance,
    George

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,275
    I do not agree that there is resource leak, since if we met with bad_alloc in new int[], there is no memory allocated at all, so no root of memory/resource leak
    I think you are correct, except that this assumes that the constructor body itself does not have code that can throw an exception. If the "..." code does throw an exception, the situation would be such that there cannot be a corresponding delete[], which is a Bad Thing.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Sorry laserlight,


    I do not fully agree. If instance B is constructed correctly, and in other part of constructor of class C, there is an exception, B's destructor will be invoked and in B's destructor, we could free the memory using delete[].

    Any comments? If I am wrong, please feel free to correct me.

    Quote Originally Posted by laserlight View Post
    I think you are correct, except that this assumes that the constructor body itself does not have code that can throw an exception. If the "..." code does throw an exception, the situation would be such that there cannot be a corresponding delete[], which is a Bad Thing.

    regards,
    George

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,275
    If instance B is constructed correctly, and in other part of constructor of class C, there is an exception, B's destructor will be invoked and in B's destructor, we could free the memory using delete[].
    I think that's correct, but it proves the point: 'whenever possible use "resource acquisition is initialization"'. Notice that the recommendation is that "if you must acquire resources in another fashion, don't do it in the constructor initializer" (emphasis mine).

    If you think that the example is a poor one, just add another pointer member to C, and then use new int[n] to initialise it. In such a case RAII is not used, so the "horrible!" comment surely applies.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi laserlight,


    Why it is horrible? Any potential issues, like memory leak?

    Quote Originally Posted by laserlight View Post
    I think that's correct, but it proves the point: 'whenever possible use "resource acquisition is initialization"'. Notice that the recommendation is that "if you must acquire resources in another fashion, don't do it in the constructor initializer" (emphasis mine).

    If you think that the example is a poor one, just add another pointer member to C, and then use new int[n] to initialise it. In such a case RAII is not used, so the "horrible!" comment surely applies.

    regards,
    George

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,275
    Why it is horrible? Any potential issues, like memory leak?
    Yes. It pretty much is related to your other thread on Challenging GotW 66's moral.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Yes, laserlight. :-)


    And I think in the thread you pointed out, there is no memory leak. :-)

    Quote Originally Posted by laserlight View Post
    Yes. It pretty much is related to your other thread on Challenging GotW 66's moral.

    regards,
    George

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,275
    And I think in the thread you pointed out, there is no memory leak. :-)
    ... if we are lucky.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks laserlight,


    I agree. I have decided to change the function try block to local try block. :-)

    Quote Originally Posted by laserlight View Post
    ... if we are lucky.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 23
    Last Post: 04-20-2009, 08:35 AM
  2. Writing Code
    By ILoveVectors in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2005, 01:27 AM
  3. Any good places for free source code?
    By VegasSte in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-19-2002, 04:22 AM
  4. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Linux Programming
    Replies: 0
    Last Post: 10-14-2002, 02:30 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21