Hi all,
struct S
{
int x;
S(int y): x(y) { } // 1
S(int y) { x = y; } // 2
}
Is there any difference between 1 and 2 regarding code optimizations that
the compiler may apply?
Best regards,
Marcin 10 1413
Marcin Kalicinski posted: Hi all,
struct S { int x; S(int y): x(y) { } // 1 S(int y) { x = y; } // 2 }
Is there any difference between 1 and 2 regarding code optimizations that the compiler may apply?
Best regards, Marcin
Not of which I am aware. I believe the sole reason that we have the first
form is for the initialization of const objects and references, eg.
struct S
{
const int x;
S(int y) : x(y) { } //1
S(int y) { x = y } //2 Compile ERROR
};
As for one being more efficient than the other, I can't think of any reason
off the top of my head.
-JKop
"JKop" <NU**@NULL.NULL> wrote in message
news:XS*****************@news.indigo.ie... Marcin Kalicinski posted:
Hi all,
struct S { int x; S(int y): x(y) { } // 1 S(int y) { x = y; } // 2 }
Is there any difference between 1 and 2 regarding code optimizations
that the compiler may apply?
[snip]
As for one being more efficient than the other, I can't think of any
reason off the top of my head.
Writing:
S::S( int y ){ x = y; }
Actually is processed as:
S::S( int y ):x(){ x = y; }
So you are default(or is it value) initializing, then assigning a value.
Which is less efficient than:
S::S( int y ):x(y){}
Jeff F
"JKop" <NU**@NULL.NULL> wrote in message
news:XS*****************@news.indigo.ie... As for one being more efficient than the other, I can't think of any
reason off the top of my head.
The FAQ addresses this http://www.parashift.com/c++-faq-lit....html#faq-10.6
Regards,
Sumit.
"Marcin Kalicinski" <ka****@poczta.onet.pl> wrote in message
news:cb**********@korweta.task.gda.pl... Hi all,
struct S { int x; S(int y): x(y) { } // 1 S(int y) { x = y; } // 2 }
Is there any difference between 1 and 2 regarding code optimizations that the compiler may apply?
The compiler may apply any optimizations at all, as
long as the 'as if' rule is observed.
But note that // 1 and // 2 above express different things.
The former initializes the member 'x', the latter does not,
but assigns 'x' a value after it is created.
This can make a difference:
S(int y) : x(y)
{
int i = x; /* OK, 'x' has a value */
}
S(int y)
{
/* suppose we forget to assign 'x' a value */
int i = x; /* OOPS, undefined behavior */
}
Also the intitialization-list form is required for certain
things, such as initialization of const or reference members,
or base class portions of objects.
-Mike
"Jeff Flinn" <NO****@nowhere.com> wrote in message
news:cb**********@bluegill.adi.com... "JKop" <NU**@NULL.NULL> wrote in message news:XS*****************@news.indigo.ie... Marcin Kalicinski posted:
Hi all,
struct S { int x; S(int y): x(y) { } // 1 S(int y) { x = y; } // 2 }
Is there any difference between 1 and 2 regarding code optimizations that the compiler may apply? [snip]
As for one being more efficient than the other, I can't think of any reason off the top of my head.
Writing:
S::S( int y ){ x = y; }
Actually is processed as:
S::S( int y ):x(){ x = y; }
No. (well a compiler is not disallowed from doing
it that way, but it's not required).
'x' is not necessarily initialized prior to entering
the constructor body. The only time that would be
guaranteed to happen is if 'x' were of a user-defined
type with a default constructor (type 'int' is not). So you are default(or is it value) initializing, then assigning a value. Which is less efficient than:
S::S( int y ):x(y){}
The efficiency of one form over another is implementation-dependent.
-Mike
"Jeff Flinn" <NO****@nowhere.com> wrote in message news:cbheua$dd8 Writing:
S::S( int y ){ x = y; }
Actually is processed as:
S::S( int y ):x(){ x = y; }
So you are default(or is it value) initializing, then assigning a value. Which is less efficient than:
S::S( int y ):x(y){}
Not true. In the first initialization, the system does call the default
constructor for user defined types like std::string, and thus it is only
less efficient when your class holds a std::string or other user type. But
for fundamental types like int, the system does not call the default
constructor, and thus it does not call the default constructor for x. Thus
both methods are equivalent, and compilers typically handle both ways
exactly the same. This rule is present for historical reasons. Exception:
if the instance of the object you declare is at static or global scope, then
the system does call the default constructor of fundamental types (but it
does not for an S object declared as a local variable).
"Mike Wahler" <mk******@mkwahler.net> wrote in message news:03YCc.602 S::S( int y ):x(){ x = y; } No. (well a compiler is not disallowed from doing it that way, but it's not required).
For user types, class x may not even have a default constructor, so in
general the compiler cannot do it this way.
'x' is not necessarily initialized prior to entering the constructor body. The only time that would be guaranteed to happen is if 'x' were of a user-defined type with a default constructor (type 'int' is not).
Not exactly true on "if 'x' were of a user-defined type with a default
constructor ". 'x' may be a user defined type without a default
constructor, or any constructors, but it contains objects that have a
default constructor, so in that case there is a compiler generated default
constructor that calls the default constructor of the contained objects.
struct X {
std::string s;
};
S::S(const X& x2) { x = x2; }
// above inefficient: same as
S::S(const X& x2) : x() { x = x2; }
// which is practically same as (though the syntax is illegal in C++)
S::S(const X& x2) : x.s() { x = x2; }
Also, if you declare your class S at global or static scope you get zero
initialization, even if 'x' is a fundamental type. Also a strange rule for
historical reasons.
"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
news:Qu********************@bgtnsc04-news.ops.worldnet.att.net... "Mike Wahler" <mk******@mkwahler.net> wrote in message news:03YCc.602
S::S( int y ):x(){ x = y; } No. (well a compiler is not disallowed from doing it that way, but it's not required).
For user types, class x may not even have a default constructor, so in general the compiler cannot do it this way.
'x' is not necessarily initialized prior to entering the constructor body. The only time that would be guaranteed to happen is if 'x' were of a user-defined type with a default constructor (type 'int' is not).
Not exactly true on "if 'x' were of a user-defined type with a default constructor ". 'x' may be a user defined type without a default constructor, or any constructors, but it contains objects that have a default constructor, so in that case there is a compiler generated default constructor that calls the default constructor of the contained objects.
Right. My reply was incomplete. Thanks for the elaboration. struct X { std::string s; };
S::S(const X& x2) { x = x2; } // above inefficient: same as S::S(const X& x2) : x() { x = x2; } // which is practically same as (though the syntax is illegal in C++) S::S(const X& x2) : x.s() { x = x2; }
Also, if you declare your class S at global or static scope you get zero initialization, even if 'x' is a fundamental type. Also a strange rule
for historical reasons.
Also correct. Since OP didn't give context about that, I suppose I
should have addressed the 'static vs 'automatic' issue.
-Mike
JKop <NU**@NULL.NULL> wrote in message news:<XS*****************@news.indigo.ie>... struct S { const int x; S(int y) : x(y) { } //1 S(int y) { x = y } //2 Compile ERROR };
As for one being more efficient than the other, I can't think of any reason off the top of my head.
The second form default-constructs the member 'x' and then calls its
assignment operator to copy the value of 'y' into 'x'.
If the default constructor or assignment operator of 'x' were to be a
heavy-duty operation with side effects, say, allocating memory from
the free store, you would notice a difference.
Prefer initialization over the default-constructor-and-assignment
route where possible.
--
Stephen M. Webb st**********@bregmasoft.com
"Marcin Kalicinski" <ka****@poczta.onet.pl> дÈëÏûÏ¢ÐÂÎÅ
:cb**********@korweta.task.gda.pl... Hi all,
struct S { int x; S(int y): x(y) { } // 1 S(int y) { x = y; } // 2 }
Is there any difference between 1 and 2 regarding code optimizations that the compiler may apply?
Best regards, Marcin
I think it's nothing about optimization, but the tow constructor have
different behavior when construct an object. For constructor 1, the x will
be initialized before enter
constructor, for a statement like
S ps = new S(1);
the compiler will generate code like
ps = operater new (sizeof(S)); // Allocate
memory
ps->x = 1;
ps->S(1);
The difference between the two constructor will be very subtle when you
construct some more complex object witch in turn contain other objects or
you want to make your
constructor "exception free". The book "Exceptional C++" gives many deep
discussion of these two kind of construcors.
Tao Bo This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jacek Dziedzic |
last post by:
Hi!
A) Why isn't it possible to set a member of the BASE class in
an initialization list of a DERIVED class constructor (except for
'calling' the base constructor from there, of course)? I even...
|
by: Thomas Lorenz |
last post by:
Hello,
I'm a little confused about object initialization. According to Stroustrup
(The C++ Programming Language, Special Edition, Section 10.2.3) constructor
arguments should be supplied in one...
|
by: Jef Driesen |
last post by:
Hello,
Suppose I have a class that looks like this:
class point {
protected:
unsigned int m_x, m_y; // OR unsigned int m_data;
public:
point();
point(unsigned int x, unsigned int y);
|
by: Jacek Dziedzic |
last post by:
Hello!
Suppose I have a class Foo that defines a default c'tor
that initializes some data using an initialization list:
Foo::Foo() : member1(0), member2(0), member3(NULL), member4(20)
// and...
|
by: anujanujdhamija |
last post by:
I am initializing a class variable using a temporary, example:
abc a1, a2;
abc a3 = a1+a2; (See prog below)
I expect a copy constructor to be invoked for initialization of a3. So
in all, I...
|
by: wkaras |
last post by:
I've compiled this code:
const int x0 = 10;
const int x1 = 20;
const int x2 = 30;
int x = { x2, x0, x1 };
struct Y
{
|
by: lovecreatesbea... |
last post by:
K&R 2, sec 2.4 says: If the variable in question is not automatic, the
initialization is done once only, conceptually before the program
starts executing, ... .
"Non-automatic variables are...
|
by: copx |
last post by:
I don't know what to think of the following..
(from the dietlibc FAQ)
Q: I see lots of uninitialized variables, like "static int foo;". What
gives?
A: "static" global variables are initialized...
|
by: George |
last post by:
I am trying to implement postponed initialization (object is not initialize
till requested).
public class clsStore
{
volatile List<clsPictureGroup_lstPictureGroups = null;
public...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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...
|
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,...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |