473,804 Members | 2,292 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Constructor initialization lists

Hello,

Suppose I have a class that looks like this:

class point {
protected:
unsigned int m_x, m_y; // OR unsigned int m_data[2];
public:
point();
point(unsigned int x, unsigned int y);
point(const point& p);
point& operator= (const point& p);
};

I know it is preferable to use initialization lists to initialize member
variables in a constructor:

point::point(un signed int x, unsigned int y)
: m_x(x), m_y(y) {}
{
}

over assignments within the constructor body:

point::point(un signed int x, unsigned int y)
{
m_x = x;
m_y = y;
}

In the second example, the default constructor for each data member is
invoked prior to the assignment in the constructor body. This will
result in lower performance (overhead of calling the unnecessary default
constructor).

But what if I replace the data members with an array? Now I can only use
assignment in the constructor body:

point::point(un signed int x, unsigned int y)
{
m_data[0] = x;
m_data[1] = y;
}

My questions are:
(1) How is m_data initialized *before* the assignment in the body?
(2) Will this result in lower performance?
Jul 22 '05 #1
8 1961
Jef Driesen wrote:
[snip]
But what if I replace the data members with an array? Now I can only use
assignment in the constructor body:
Right.
That's because arrays are what we call 'half baked types'. Arrays behave
differently in many aspects then other types.

point::point(un signed int x, unsigned int y)
{
m_data[0] = x;
m_data[1] = y;
}

My questions are:
(1) How is m_data initialized *before* the assignment in the body?
In this specific case: Not at all. int, unsigned int, char, float, double,
pointers (the 'builtin types') are *not* default initialized at all. They
simply contain what was in memory before the variable was placed there.
Usually its just garbage.

If you have an array of some class type however (eg. std::string), the default
constructor will be called for each array member.
(2) Will this result in lower performance?


Depends: If you have an array of builtin type, then no. Usually this won't
result in lower performance. If however the default constructor is run for
each array member, then yes, most likely this will result in lower performance.
It depends on what the default constructor has to do.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #2
On Tue, 21 Dec 2004 10:01:04 +0100, Jef Driesen
<je********@hot mail.com.nospam > wrote:
Hello,

Suppose I have a class that looks like this:

class point {
protected:
unsigned int m_x, m_y; // OR unsigned int m_data[2];
public:
point();
point(unsigned int x, unsigned int y);
point(const point& p);
point& operator= (const point& p);
};

I know it is preferable to use initialization lists to initialize member
variables in a constructor:

point::point(u nsigned int x, unsigned int y)
: m_x(x), m_y(y) {}
{
}

over assignments within the constructor body:

point::point(u nsigned int x, unsigned int y)
{
m_x = x;
m_y = y;
}

In the second example, the default constructor for each data member is
invoked prior to the assignment in the constructor body...
POD-types are NOT default constructed or initialized, their value is
undefined. In order to default initialize an unsigned int, you must
write:

point::point(un signed int x, unsigned int y)
: m_x(), m_y()
{
m_x = x;
m_y = y;
}
... This will result in lower performance (overhead of calling
the unnecessary default constructor).
For unsigned int, there is no performance loss to speak of. For
non-POD types, there might be ... depends on how the default
constructor (if any) and operator= (if overloaded) is implemented.
Also, it depends on how the compiler might optimize things. You have
to test to make sure.
But what if I replace the data members with an array? Now I can only use
assignment in the constructor body:

point::point(u nsigned int x, unsigned int y)
{
m_data[0] = x;
m_data[1] = y;
}

My questions are:
(1) How is m_data initialized *before* the assignment in the body?
It isn't. The compiler only reserves storage for an array of two
unsigned ints. Its contents are initially garbage. Of course, if the
members are of non-POD type, they must be default constructible, in
which case the default constructor is called once for each element.
But unsigned int is POD.
(2) Will this result in lower performance?


See my remarks above. You won't know until you have tested it.

(I believe it was Donald Knuth who said: "Premature optimization is
the root of all evil.")

--
Bob Hairgrove
No**********@Ho me.com
Jul 22 '05 #3
Bob Hairgrove wrote:

My questions are:
(1) How is m_data initialized *before* the assignment in the body?

It isn't. The compiler only reserves storage for an array of two
unsigned ints. Its contents are initially garbage. Of course, if the
members are of non-POD type, they must be default constructible, in
which case the default constructor is called once for each element.
But unsigned int is POD.


I think the OP meant "how can I initialize m_data *before*..."
rather than "how is it that m_data gets initialized".

to the OP:
There is no way to initialize arrays inside the constructor
init-list. The same goes for members of structs.
(2) Will this result in lower performance?


Don't worry about it if these are variables of built-in types.
Only if their default-construction is time-costly, you should
care.

HTH,
- J.
Jul 22 '05 #4
Karl Heinz Buchegger wrote:
In this specific case: Not at all. int, unsigned int, char, float, double,
pointers (the 'builtin types') are *not* default initialized at all. They
simply contain what was in memory before the variable was placed there.
Usually its just garbage.


Actually, they might or might not be default initialized. It depends how
the storage is allocated, such is the inconsistant nature of C++.
Jul 22 '05 #5
Karl Heinz Buchegger wrote:
Jef Driesen wrote:

[snip]
But what if I replace the data members with an array? Now I can only use
assignment in the constructor body:

Right.
That's because arrays are what we call 'half baked types'. Arrays behave
differently in many aspects then other types.

point::point( unsigned int x, unsigned int y)
{
m_data[0] = x;
m_data[1] = y;
}

My questions are:
(1) How is m_data initialized *before* the assignment in the body?

In this specific case: Not at all. int, unsigned int, char, float, double,
pointers (the 'builtin types') are *not* default initialized at all. They
simply contain what was in memory before the variable was placed there.
Usually its just garbage.

If you have an array of some class type however (eg. std::string), the default
constructor will be called for each array member.

(2) Will this result in lower performance?

Depends: If you have an array of builtin type, then no. Usually this won't
result in lower performance. If however the default constructor is run for
each array member, then yes, most likely this will result in lower performance.
It depends on what the default constructor has to do.


Thanks for the detailed answer.
Jul 22 '05 #6
Bob Hairgrove wrote:
POD-types are NOT default constructed or initialized, their value is
undefined. In order to default initialize an unsigned int, you must
write:

point::point(un signed int x, unsigned int y)
: m_x(), m_y()
{
m_x = x;
m_y = y;
}


I didn't know I could default initialize a POD-type this way.
(2) Will this result in lower performance?

See my remarks above. You won't know until you have tested it.

(I believe it was Donald Knuth who said: "Premature optimization is
the root of all evil.")


I understand that, but I wanted to know what the compiler is actually
doing with the array. And I think it won't hurt to remove unnecessary
initializations if possible.
Jul 22 '05 #7
OTOH, I don't know who originally wrote this, but "Belated
pessimization is the leaf of no good." :)

I hope I am not insulting your expertise and intelligence, but Herb
Sutter (http://www.gotw.ca) makes a very good argument for
internalizing practices that don't unnecessarily burden code with
"pessimizations " (in his and Andrei Alexandrescu's great book, "C++
Coding Standards : 101 Rules, Guidelines, and Best Practices").

Good examples of this would be using initializer lists to avoid
unwanted default constructions, and passing objects by reference to
avoid the unwanted cost of copying the objects. In no way can things
like this be construed as "premature optimization". It's simply good
C++ practice.

Of course, poring over every line of code to wring the last drop of
performance out of it, or replacing code with assembly language, before
knowing exactly where the real bottlenecks are, is genuine premature
optimization.

Jul 22 '05 #8
On 21 Dec 2004 22:00:49 -0800, "Edwin Fine, Fine Computer Consultants"
<em*****@usa.ne t> wrote:
OTOH, I don't know who originally wrote this, but "Belated
pessimizatio n is the leaf of no good." :)

I hope I am not insulting your expertise and intelligence, but Herb
Sutter (http://www.gotw.ca) makes a very good argument for
internalizin g practices that don't unnecessarily burden code with
"pessimization s" (in his and Andrei Alexandrescu's great book, "C++
Coding Standards : 101 Rules, Guidelines, and Best Practices").

Good examples of this would be using initializer lists to avoid
unwanted default constructions, and passing objects by reference to
avoid the unwanted cost of copying the objects. In no way can things
like this be construed as "premature optimization". It's simply good
C++ practice.

Of course, poring over every line of code to wring the last drop of
performance out of it, or replacing code with assembly language, before
knowing exactly where the real bottlenecks are, is genuine premature
optimization .


I also use initialization lists as much as possible because I know
that it is good practice and avoids wasting work for objects with
non-trivial default constructors.

The OP was worried about the performance penalty from the extra
initialization of an unsigned int, though. It seemed to fit in with
your example: "... poring over every line of code to wring the last
drop of performance out of it".

(PS - I'll always be glad for anyone to "insult my intelligence" with
Herb Sutter's advice! <g>)

--
Bob Hairgrove
No**********@Ho me.com
Jul 22 '05 #9

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

Similar topics

10
7237
by: jeffc | last post by:
When compiling the following program, I get an error on the line specified: The base class "B" cannot be initialized because it does not have a default constructor. If I remove the "virtual" inheritance of B (so that it uses plain inheritance), it compiles fine. It also compiles if I invoke the constructor for B explicitly, as shown in the comment after the error. I'm not aware of any reason that virtual inheritance should be special...
1
4454
by: Eric C | last post by:
I'm reading a c++ book by Eric Nagler named Learning C++ and I ran into an exercise that requires a cpp file that defines several member functions of a header. Here is my problem, it defines a constructor that requires two enum types to be initialized. I haven't been able to figure it out. Here is the code for the header: -------------------- #define CARD_H class Card {
15
3362
by: Alfonso Morra | last post by:
Hi, I have some code from an example, that I want to retrofit into my project. The code from the example has the following line: SharedAppenderPtr myAppender( new RollingFileAppender("MyAppender")) I want to move this line of code to a constructor in a wrapper class, so that I can determine the appropriate Appender name during the object's
4
1802
by: david | last post by:
Hello, I'd like to know whether the compiler can detect a member variable which is not initialized (on purpose) by the constructor. Easier with an simple example (the class is supposed to be a fixed point variable): class A {
22
3632
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float v);
20
432
by: royashish | last post by:
Hi all , A question to the C++ fraternity , Why the Copy constructor ? Was suggested in Effective C++ , In Case the Object contains a Char* , a assignment operator = makes the two object point to the same address . In this case any Of the two Object is destroyed a memory leak occurs as one of the object stays on the Heap . My experiments on the same have proved otherwise .
5
8203
by: Pallav singh | last post by:
How can we justify that initializer list is better in performance than assignment list in constructor of C++ ??
16
4198
by: Ray | last post by:
Hi all, After many years of C, I thought I'd move to C++ recently. I think because I think in C, I'm coming to a misunderstanding of something. I've created a class foo which contains a private variable which is a priority queue. In class foo's header file, I declared it as: class foo { private:
6
3023
by: Taras_96 | last post by:
Hi everyone, The FAQ at http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6 states that: "Consider the following constructor that initializes member object x_ using an initialization list: Fred::Fred() : x_(whatever) { }. The most common benefit of doing this is improved performance. For example, if the expression whatever is the same type as member variable x_, the result of the whatever expression is constructed
0
9591
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10594
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10343
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10331
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9166
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5667
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.