Thread: Why it is not good code for constructor

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    And I think in the thread you pointed out, there is no memory leak. :-)
    ... if we are lucky.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 07:35 AM
  2. Writing Code
    By ILoveVectors in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2005, 12: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, 01:30 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM