473,692 Members | 2,529 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Default Initialization Vs. Value Initialization

What's the difference between them?

Take the following:

#include <iostream>
struct Blah
{
int k;
const char* p_b;
std::string c;
};
I used to think that when you did:
Blah poo = Blah();
that all its members got "default initialized", which resulted in:

poo.k == 0
poo.p_b == 0 (null pointer value)
poo.c (Gets the default std::string constructor called with no arguments)
I'm correct in the above, yes? But now I hear that things have changed and
that

Blah poo = Blah();

results in "value intialization". ..

So what's "value intialization" and how is it different from "default
initialization" ?
-JKop
Jul 22 '05 #1
10 4191

"JKop" <NU**@NULL.NULL > wrote in message news:oJ******** ***********@new s.indigo.ie...
So what's "value intialization" and how is it different from "default
initialization" ?


To understand value initialization, you have to understand that while POD types have
default initialization (zero initialization) , some times the default initialization is just omitted.
[This I see as a major defect in the language, but it makes it performance compatible
with C so it's hard to convince people of the stupidity.]

The problem is that types such as:
struct S {
int i;
T t;
};

Get different behavior in the old standard based on the type of T (if T is POD then S is
POD and you get zero initialization and i is hence zero initialized, if T is not POD, then i doesn't
get initialized because S's default constructor doesn't touch it).

To fix this, most places where default initialization occured are replaced with value initialization.
And value initialization rather than using "PODness" of a determining factor looks for the presence
of a user defined constructor to determine whether to recursively value-initialize or to just run the
consturctor...

Jul 22 '05 #2
Ron Natalie posted:

"JKop" <NU**@NULL.NULL > wrote in message
news:oJ******** ***********@new s.indigo.ie...
So what's "value intialization" and how is it different from "default initialization" ?

To understand value initialization, you have to

understand that while POD types have default initialization (zero initialization) , some times the default initialization is just omitted. [This I see as a major defect in the language, but it makes it performance compatible with C so it's hard to convince people of the stupidity.]

The problem is that types such as:
struct S {
int i;
T t;
};

Get different behavior in the old standard based on the type of T (if T is POD then S is POD and you get zero initialization and i is hence zero initialized, if T is not POD, then i doesn't get initialized because S's default constructor doesn't touch it).

To fix this, most places where default initialization occured are replaced with value initialization. And value initialization rather than using "PODness" of a determining factor looks for the presence of a user defined constructor to determine whether to recursively value-initialize or to just run the consturctor...

Okay... I think I understand. Given:

struct Blah
{
int i;
std::string k;
};
With regard to the *old* standard, when you did:

Blah poo = Blah();

Then "poo.i" didn't get... let's just call it
"initialize d".
But... with the new standard, when you do:

Blah poo = Blah();

then "poo.i" *does* get initialized.
BTW, Blah is a POD, right? I'd classify it as so in
anyway...
Would I be right in thinking that the following are the
factors which render a structure *no longer* a POD:

A) a constructor (including a copy constructor)
B) private or protected members

Well... if you look at Blah, it has neither, hence you can
define one as so:

std::string a("MONKEY!");

Blah poo = { 5, a };

And as such it's a POD... right?
-JKop
Jul 22 '05 #3

"JKop" <NU**@NULL.NULL > wrote in message
news:vV******** ***********@new s.indigo.ie...
Would I be right in thinking that the following are the
factors which render a structure *no longer* a POD:

A) a constructor (including a copy constructor)
Not relevant. Even if you don't supply a constructor, the compiler will
supply one.
B) private or protected members


Also not relevant.

A POD type is either a built-in type, or contains only built-in types, or
contains only POD members (not pointer to such) or arrays of such. (I think
I've covered it, but maybe not. It's been described many times..try
searching on groups.google.c om.)

-Howard
Jul 22 '05 #4

"JKop" <NU**@NULL.NULL > wrote in message news:vV******** ***********@new s.indigo.ie...

With regard to the *old* standard, when you did:

Blah poo = Blah();

Then "poo.i" didn't get... let's just call it
"initialize d".
But... with the new standard, when you do:
then "poo.i" *does* get initialized.
Correct.

BTW, Blah is a POD, right? I'd classify it as so in
anyway...
No, Blah is NOT POD, which is the problem. It it had been POD,
then the default initialization behavior would have been zero initialization.
Lets say you had
struct PODBlah {
int i;
char* c;
};
PODBlah poo = PODBlah();

Both PODBlah.i and PODBlah.c get default initalized because PODBlah is POD.

Would I be right in thinking that the following are the
factors which render a structure *no longer* a POD:

A) a constructor (including a copy constructor)
B) private or protected members
You missed one important one. It can't have any non-static
data members of non-POD type. Since std::string is not POD,
then any class that contains it is also NOT POD (although it may
be an aggregate).

A POD-class is an aggregate that has no non-static data members that
aren't POD.

Aggregate classes have no user declared constructors, no private or protected
non-static data members, no base classes, and no virtual functions.
std::string a("MONKEY!");

Blah poo = { 5, a };

And as such it's a POD... right?


Nope, it's an aggregate (and hence you can use the { } aggregate initializer syntax).
But it's not POD.

Jul 22 '05 #5

"Howard" <al*****@hotmai l.com> wrote in message news:t%h4d.6204 93$Gx4.316743@b gtnsc04-> A POD type is either a built-in type, or
contains only built-in types, or
contains only POD members (not pointer to such) or arrays of such. (I think
I've covered it, but maybe not. It's been described many times..try
searching on groups.google.c om.)


Not.
Jul 22 '05 #6
> A POD type is either a built-in type, or contains only built-in types,
or contains only POD members (not pointer to such) or arrays of such.
(I think I've covered it, but maybe not. It's been described many
times..try searching on groups.google.c om.)

That's a paradox if I've ever seen one.

A house is made of bricks, but bricks are still made of molecules!

Similarly, a structure may be made up of other structures, but these other
structures are still made up of intrinsic types.
-JKop
Jul 22 '05 #7

"Ron Natalie" <ro*@sensor.com > wrote in message
news:41******** *************** @news.newshosti ng.com...

"Howard" <al*****@hotmai l.com> wrote in message news:t%h4d.6204 93$Gx4.316743@b gtnsc04-> A POD type is either a built-in
type, or contains only built-in types, or
contains only POD members (not pointer to such) or arrays of such. (I think I've covered it, but maybe not. It's been described many times..try
searching on groups.google.c om.)


Not.


:-)

Not the best explanation there, Ron. But I've read your other post, and
that explains it better. I wasn't aware of the user-created constructor
being relevant to the issues though. Any idea why that's relevant? Because
it interferes with the default initialization scheme?

-Howard
Jul 22 '05 #8
In article <vV************ *******@news.in digo.ie>,
JKop <NU**@NULL.NULL > wrote:
Would I be right in thinking that the following are the
factors which render a structure *no longer* a POD:


Check out http://www.comeaucomputing.com/techtalk/#pod
--
Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Jul 22 '05 #9

"Howard" <al*****@hotmai l.com> wrote in message news:91k4d.6208 94$Gx4.351425@b gtnsc04-
:-)

Not the best explanation there, Ron. But I've read your other post, and
that explains it better. I wasn't aware of the user-created constructor
being relevant to the issues though. Any idea why that's relevant? Because
it interferes with the default initialization scheme?


You have to realize that that is a requirement inheritted from aggregates. You can't
have a user declared construtor as how would that boogie with the aggregate initialization
sequence (you can't both have a constructor and a member-wise constructor).

Jul 22 '05 #10

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

Similar topics

49
2606
by: Mark Hahn | last post by:
As we are addressing the "warts" in Python to be fixed in Prothon, we have come upon the mutable default parameter problem. For those unfamiliar with the problem, it can be seen in this Prothon code sample where newbies expect the two function calls below to both print : def f( list= ): print list.append!(1) f() # prints
4
4730
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC 14882:2003(E) §8.5 says:   To zero-initialize an object of type T means: 5   -- if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T;
9
3713
by: Pierre Senellart | last post by:
The C++ standard states (26.3.2.1), about std::valarray constructors: > explicit valarray(size_t); > > The array created by this constructor has a length equal to the value of > the argument. The elements of the array are constructed using the default > constructor for the instantiating type T. Does that mean that, on built-in types (e.g. int), "default initialization" (as described in 8.5) is performed?
3
6632
by: countd4 | last post by:
I have built a working user control. However, to make it work, I always have to set certian properties using the properties sheet for the control when using it on other forms. I want to be able to set default values for most properties so the control will work as-is without requiring the developer to set them. I notice in the compment initialization code create when I drop the control on a form that all the properties are set to null or...
10
8548
by: utab | last post by:
Dear all, Can somebody direct me to some resources on the subject or explain the details in brief? I checked the FAQ but could not find or maybe missed. Regards,
4
1624
by: shapper | last post by:
Hello, I am creating a class where I have various properties. How to I set a default property value in case the property is not defined by the user. For example, I have the property: ' Priority Public Property Priority() As Mail.MailPriority
74
15985
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the creation of this implicit default constructor, to force the creation of a struct via my constructor only? Zytan
23
3656
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for the built-in types, the value is usually garbage. Is this right? However, I'm a bit confused about value-initialization, when does it happen, and what kind of values are assigned to objects?
4
3708
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and value-initialization. I think default-init calls default constructor for class objects and sets garbage values to PODs. Value-init also calls default constructor for class objects and sets 0s to POD types. This is what I've learned from the books (especially...
0
9090
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
8970
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
8812
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
8810
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6462
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5822
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4329
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
4564
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2984
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

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.