473,382 Members | 1,407 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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

Expand|Select|Wrap|Line Numbers
  1. C::C(int)
  2. try
  3. : B(new int[n]) // horrible!
  4. {
  5. ...
  6. }
  7. catch(Error &e)
  8. {
  9.  
  10. }
  11.  


thanks in advance,
George
Dec 26 '07 #1
10 1607
On 2007-12-26 09:10:47 -0500, George2 <ge*************@yahoo.comsaid:
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

Expand|Select|Wrap|Line Numbers
  1. C::C(int)
  2. try
  3.    : B(new int[n]) // horrible!
  4. {
  5.    ...
  6. }
  7. catch(Error &e)
  8. {
  9. }
  10.  

thanks in advance,
George
If an exception of type Error is thrown inside the constructor's try
block, will the object be fully initialized on return?

--

-kira

Dec 26 '07 #2
On Wed, 26 Dec 2007 09:34:24 -0500, Kira Yamato wrote:
On 2007-12-26 09:10:47 -0500, George2 <ge*************@yahoo.comsaid:
>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

Expand|Select|Wrap|Line Numbers
  1. C::C(int)
  2. try
  3.    : B(new int[n]) // horrible!
  4. {
  5.    ...
  6. }
  7. catch(Error &e)
  8. {
  9. }

thanks in advance,
George

If an exception of type Error is thrown inside the constructor's try
block, will the object be fully initialized on return?
The only return from constructor's try block can be via an exception, so
it doesn't matter.

--
Tadeusz B. Kopec (tk****@NOSPAMPLEASElife.pl)
"Be there. Aloha."
-- Steve McGarret, _Hawaii Five-Oh_
Dec 26 '07 #3


George2 wrote:
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

Expand|Select|Wrap|Line Numbers
  1. C::C(int)
  2. try
  3.    : B(new int[n]) // horrible!
  4. {
  5.    ...
  6. }
  7. catch(Error &e)
  8. {
  9. }
  10.  

thanks in advance,
George
This does an anonymous memory allocation, so how would you ever reclaim
the memory once it's allocated? Whenever you allocate memory with 'new',
you should reclaim the memory with 'delete' once you're done with it.

Bob Terwilliger
Dec 26 '07 #4
On Wed, 26 Dec 2007 06:10:47 -0800, George2 wrote:
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

Expand|Select|Wrap|Line Numbers
  1. C::C(int)
  2. try
  3.    : B(new int[n]) // horrible!
  4. {
  5.    ...
  6. }
  7. catch(Error &e)
  8. {
  9. }
  10.  
Whether it's good code for constructor or not depends on what B is. If B
stands for boost::scoped_array the code is absolutely OK. Generally the
problem is: when B's constructor throws what happens with the allocated
memory. But this code suggests that B takes ownership of it so I don't
see problem. If B's constructor throws it should free the memory.

--
Tadeusz B. Kopec (tk****@NOSPAMPLEASElife.pl)
I was making donuts and now I'm on a bus!
Dec 26 '07 #5
On 2007-12-26 16:40:30 -0500, "Tadeusz B. Kopec"
<tk****@NOSPAMPLEASElife.plsaid:
On Wed, 26 Dec 2007 09:34:24 -0500, Kira Yamato wrote:
>On 2007-12-26 09:10:47 -0500, George2 <ge*************@yahoo.comsaid:
>>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

Expand|Select|Wrap|Line Numbers
  1. C::C(int)
  2. try
  3. : B(new int[n]) // horrible!
  4. {
  5. ...
  6. }
  7. catch(Error &e)
  8. {
  9. }

thanks in advance,
George

If an exception of type Error is thrown inside the constructor's try
block, will the object be fully initialized on return?

The only return from constructor's try block can be via an exception, so
it doesn't matter.
No. Had the constructor returned via exception, the object will be
outside the scope of its declaration.

In his code, the constructor catches the exception and returns
normally. His object remains in scope. The question I asked him is if
his object is valid or not.

--

-kira

Dec 27 '07 #6
On 2007-12-26 16:59:08 -0500, "Tadeusz B. Kopec"
<tk****@NOSPAMPLEASElife.plsaid:
On Wed, 26 Dec 2007 06:10:47 -0800, George2 wrote:
>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

Expand|Select|Wrap|Line Numbers
  1. C::C(int)
  2. try
  3. : B(new int[n]) // horrible!
  4. {
  5. ...
  6. }
  7. catch(Error &e)
  8. {
  9. }

Whether it's good code for constructor or not depends on what B is. If B
stands for boost::scoped_array the code is absolutely OK. Generally the
problem is: when B's constructor throws what happens with the allocated
memory. But this code suggests that B takes ownership of it so I don't
see problem. If B's constructor throws it should free the memory.
Please do not encourage bad coding practices.

It is not ok. We don't know what "..." can be. If in the middle of
initializing the object as part of the "..." and an exception of
Exception is thrown, then the object may not be fully initialized.

--

-kira

Dec 27 '07 #7
On 2007-12-26 21:04:22 -0500, Kira Yamato <ki*****@earthlink.netsaid:
On 2007-12-26 16:40:30 -0500, "Tadeusz B. Kopec"
<tk****@NOSPAMPLEASElife.plsaid:
>On Wed, 26 Dec 2007 09:34:24 -0500, Kira Yamato wrote:
>>On 2007-12-26 09:10:47 -0500, George2 <ge*************@yahoo.comsaid:

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

Expand|Select|Wrap|Line Numbers
  1. C::C(int)
  2. try
  3. : B(new int[n]) // horrible!
  4. {
  5. ...
  6. }
  7. catch(Error &e)
  8. {
  9. }

thanks in advance,
George

If an exception of type Error is thrown inside the constructor's try
block, will the object be fully initialized on return?

The only return from constructor's try block can be via an exception, so
it doesn't matter.

No. Had the constructor returned via exception, the object will be
outside the scope of its declaration.

In his code, the constructor catches the exception and returns
normally. His object remains in scope. The question I asked him is if
his object is valid or not.
Alright. I retract what I said.

I did not know the behavior of the constructor when an exception is
thrown inside it.

In the OP's original code, the catch block will rethrow the exception
even if it is not explicitly told so.

--

-kira

Dec 27 '07 #8
On Wed, 26 Dec 2007 21:04:22 -0500, Kira Yamato wrote:
On 2007-12-26 16:40:30 -0500, "Tadeusz B. Kopec"
<tk****@NOSPAMPLEASElife.plsaid:
>On Wed, 26 Dec 2007 09:34:24 -0500, Kira Yamato wrote:
>>On 2007-12-26 09:10:47 -0500, George2 <ge*************@yahoo.com>
said:

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

Expand|Select|Wrap|Line Numbers
  1. C::C(int)
  2. try
  3. : B(new int[n]) // horrible!
  4. {
  5. ...
  6. }
  7. catch(Error &e)
  8. {
  9. }

thanks in advance,
George

If an exception of type Error is thrown inside the constructor's try
block, will the object be fully initialized on return?

The only return from constructor's try block can be via an exception,
so it doesn't matter.

No. Had the constructor returned via exception, the object will be
outside the scope of its declaration.

In his code, the constructor catches the exception and returns normally.
His object remains in scope. The question I asked him is if his object
is valid or not.
Sorry, I wasn't precise. You can leave the catch clause of constructor's
function-try-block only by throwing exception. Standard makes absorbing
exception there impossible so he can't return normally. Inside the catch
clause object is invalid and accessing any non-static members or base
classes is UB.

--
Tadeusz B. Kopec (tk****@NOSPAMPLEASElife.pl)
What we cannot speak about we must pass over in silence.
-- Wittgenstein
Dec 27 '07 #9
On Wed, 26 Dec 2007 21:07:38 -0500, Kira Yamato wrote:
On 2007-12-26 16:59:08 -0500, "Tadeusz B. Kopec"
<tk****@NOSPAMPLEASElife.plsaid:
>On Wed, 26 Dec 2007 06:10:47 -0800, George2 wrote:
>>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

Expand|Select|Wrap|Line Numbers
  1. C::C(int)
  2. try
  3. : B(new int[n]) // horrible!
  4. {
  5. ...
  6. }
  7. catch(Error &e)
  8. {
  9. }

Whether it's good code for constructor or not depends on what B is. If
B stands for boost::scoped_array the code is absolutely OK. Generally
the problem is: when B's constructor throws what happens with the
allocated memory. But this code suggests that B takes ownership of it
so I don't see problem. If B's constructor throws it should free the
memory.

Please do not encourage bad coding practices.

It is not ok. We don't know what "..." can be. If in the middle of
initializing the object as part of the "..." and an exception of
Exception is thrown, then the object may not be fully initialized.
Sorry, but I don't understand. What's in constructor's body is completely
irrelevant. Only important thing is what B is. If it means something that
takes ownership of the memory passed to it's constructor, for example a
member of type boost::scoped_array, it's the proper idiom for
initialising. If C's constructor body throws, all members and base
subobjects will be destroyed before reaching catch clause, and as B has
taken the ownership of the memory, it deleted it.
If, on the other hand, B doesn't take ownership of the memory, passing to
its constructor a newly allocated pointer is a big error.

--
Tadeusz B. Kopec (tk****@NOSPAMPLEASElife.pl)
Women who want to be equal to men lack imagination.
Dec 28 '07 #10
On 2007-12-28 16:18:43 -0500, "Tadeusz B. Kopec"
<tk****@NOSPAMPLEASElife.plsaid:
On Wed, 26 Dec 2007 21:07:38 -0500, Kira Yamato wrote:
>On 2007-12-26 16:59:08 -0500, "Tadeusz B. Kopec"
<tk****@NOSPAMPLEASElife.plsaid:
>>On Wed, 26 Dec 2007 06:10:47 -0800, George2 wrote:

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

Expand|Select|Wrap|Line Numbers
  1. C::C(int)
  2. try
  3. : B(new int[n]) // horrible!
  4. {
  5. ...
  6. }
  7. catch(Error &e)
  8. {
  9. }

Whether it's good code for constructor or not depends on what B is. If
B stands for boost::scoped_array the code is absolutely OK. Generally
the problem is: when B's constructor throws what happens with the
allocated memory. But this code suggests that B takes ownership of it
so I don't see problem. If B's constructor throws it should free the
memory.

Please do not encourage bad coding practices.

It is not ok. We don't know what "..." can be. If in the middle of
initializing the object as part of the "..." and an exception of
Exception is thrown, then the object may not be fully initialized.

Sorry, but I don't understand. What's in constructor's body is completely
irrelevant. Only important thing is what B is. If it means something that
takes ownership of the memory passed to it's constructor, for example a
member of type boost::scoped_array, it's the proper idiom for
initialising. If C's constructor body throws, all members and base
subobjects will be destroyed before reaching catch clause, and as B has
taken the ownership of the memory, it deleted it.
If, on the other hand, B doesn't take ownership of the memory, passing to
its constructor a newly allocated pointer is a big error.
You're right. I didn't really understand this behavior of the
constructor when an exception is thrown in it. I retract my statement
earlier.

Sorry.

--

-kira

Dec 28 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Dan Cernat | last post by:
Hi there, A few threads I had a little chat about default values. I am starting this thread because I want to hear more opinions about the default values of function parameters. Some say they...
9
by: John Cho | last post by:
// CHO, JOHN #include<iostream> class fracpri{ int whole; int numer; int denom;
29
by: Cheng Mo | last post by:
Recently I happens to read some sourcecode which has different style from my previous experience. In this style, all functions return a RETURN_TYPE_T which is defined as typedef unsigned int...
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
8
by: nrhayyal | last post by:
hi all, is it a good idea to assign object to pointers without initialising it to NULL? suppose if i have a class TEST class ELEMENT { private: int a; string s1;
0
by: Daniel Sélen Secches | last post by:
I found a good class to do a simple FTP. Very good.... I'm posting it with the message, i hope it helps someone ============================================================== Imports...
3
by: swengtoo | last post by:
In his book "More Effective C++", Scott Meyer suggests in "Item 4" to "Avoid gratuitous default constructors". To summarize his claims against default constructors for "the right classes" he...
11
by: optimistx | last post by:
http://developer.yahoo.com/yui/index.html Is this rubbish, evil, bad, wrong, not to recommend, "go away"-folks, "knitting suits them better"-people etc or something that a new javascript...
23
by: tonytech08 | last post by:
What I like about the C++ object model: that the data portion of the class IS the object (dereferencing an object gets you the data of a POD object). What I don't like about the C++ object...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.