473,386 Members | 1,630 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,386 software developers and data experts.

Object Initialisation


I have just been reading 8.5 in the Standard, and am trying to make sense
of the different kinds of initialisations.

Up until now, I thought of an object as either NOT being initialised (i.e.
containing garbage), or being default initialised (i.e. containing the
default value for that type).

Here are some examples of the former:

struct MyStruct {

int a;
double b;
void *p;
};

int main()
{
int a; /* Contains garbage */

double b; /* Contains garbage */

void *p; /* Contains garbage */

MyStruct obj; /* All members contain garbage */
}
And here are some examples of the latter:

struct MyStruct {

int a;
double b;
void *p;
};
int main()
{
int a = int(); /* Contains 0 */

double b = double(); /* Contains 0.0 */

typedef void *voidptr;
void *p = voidptr(); /* Contains null pointer value */

MyStruct obj = {}; /* Each member has the same values as above */
}

The Standard tells me that there are three kinds of initialisation:
Zero-initialisation
Default-initialisation
Value-initialisation
In my above code snippet, what kind of initialisation am I peforming? I
think it's "default initialisation", as I'm giving the object its default
value.

What is zero-initialisation, and can anyone give an example of it please?

What is value-initialisation, and can anyone give an example of it please?
Also, what kind of initialisation does the following class perform?

template<class T>
struct Init {

T obj;

Init() : obj() {}
};

--

Frederick Gotham
Jul 20 '06 #1
13 2036
Frederick Gotham wrote:
I have just been reading 8.5 in the Standard, and am trying to make
sense of the different kinds of initialisations.

Up until now, I thought of an object as either NOT being initialised
(i.e. containing garbage), or being default initialised (i.e.
containing the default value for that type).

Here are some examples of the former:

struct MyStruct {

int a;
double b;
void *p;
};

int main()
{
int a; /* Contains garbage */

double b; /* Contains garbage */

void *p; /* Contains garbage */

MyStruct obj; /* All members contain garbage */
}
And here are some examples of the latter:

struct MyStruct {

int a;
double b;
void *p;
};
int main()
{
int a = int(); /* Contains 0 */

double b = double(); /* Contains 0.0 */

typedef void *voidptr;
void *p = voidptr(); /* Contains null pointer value */

MyStruct obj = {}; /* Each member has the same values as above */
}

The Standard tells me that there are three kinds of initialisation:
Zero-initialisation
Default-initialisation
Value-initialisation
And there is the fourth kind: uninitialised.
In my above code snippet, what kind of initialisation am I peforming?
In the first part, none. Objects are _uninitialised_. In the second
part the _default_.
I think it's "default initialisation", as I'm giving the object its
default value.
Right. Only in the second case.
What is zero-initialisation, and can anyone give an example of it
please?
That's the incarnation of the default-initialisation for PODs.
What is value-initialisation, and can anyone give an example of it
please?
int a(5);
Also, what kind of initialisation does the following class perform?

template<class T>
struct Init {

T obj;

Init() : obj() {}
};
Default.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 20 '06 #2
Victor Bazarov posted:

>What is zero-initialisation, and can anyone give an example of it
please?

That's the incarnation of the default-initialisation for PODs.

The reason we invent words is not so we have a label for something, but so
that we can distinguish it from other things.

That is why I'm confused with "zero-initialsation" Vs. "default-
initialisation".

Are they not exactly the same? If so, why give them different names?

Can you please give an example of zero-initialising an object?

In the following code, what kind of initialisation takes place?

struct MyStruct {

int a;
double b;
void *p;
};

int main()
{
MyStruct static obj; /* zero or default? */
MyStruct static obj = {}; /* zero or default? */
MyStruct obj = {}; /* zero or default? */
}

--

Frederick Gotham
Jul 20 '06 #3
Frederick Gotham wrote:
Victor Bazarov posted:

>>What is zero-initialisation, and can anyone give an example of it
please?

That's the incarnation of the default-initialisation for PODs.


The reason we invent words is not so we have a label for something,
but so that we can distinguish it from other things.

That is why I'm confused with "zero-initialsation" Vs. "default-
initialisation".

Are they not exactly the same?
No. Yes. For PODs they are the same. For non-PODs they are not
the same.
If so, why give them different names?
Because they are not, in general case.
Can you please give an example of zero-initialising an object?
static int a;
void *p(0);
In the following code, what kind of initialisation takes place?

struct MyStruct {

int a;
double b;
void *p;
};

int main()
{
MyStruct static obj; /* zero or default? */
Zero.
MyStruct static obj = {}; /* zero or default? */
First zero, then default (which in this case superfluous).
MyStruct obj = {}; /* zero or default? */
Default, which for POD is zero-.
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 20 '06 #4
Victor Bazarov posted:
No. Yes. For PODs they are the same. For non-PODs they are not
the same.

Does zero-initialise mean that you explicitly write the numeral figure
zero, as in:

int i = 0;

double b = 0;

int *p = 0;

?
All the above initialisations are equivalent to:
int i = int();

double b = double();

typedef int *intptr;
int *p = intptr();
I still don't understand how zero-initialisation is any different to
default-initialisation. . .
--

Frederick Gotham
Jul 20 '06 #5
Frederick Gotham wrote:
Victor Bazarov posted:
>No. Yes. For PODs they are the same. For non-PODs they are not
the same.


Does zero-initialise mean that you explicitly write the numeral figure
zero, as in:

int i = 0;

double b = 0;

int *p = 0;

?
Sort of. '0' is an octal int literal, intialising a 'double' with it
potentially involves a temporary of type 'double' and initialising
a pointer with it potentially involves a temporary null pointer. The
compiler probably does everything behind the scenes and during compile-
time, anyway.
All the above initialisations are equivalent to:
int i = int();

double b = double();

typedef int *intptr;
int *p = intptr();
Right.
I still don't understand how zero-initialisation is any different to
default-initialisation. . .
For non-POD it involves calling the default c-tor. There are no c-tors
for PODs, and that's why there is no difference between 'default-' and
'zero-' for PODs. Just read the Standard.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 20 '06 #6

Frederick Gotham wrote:
I have just been reading 8.5 in the Standard, and am trying to make sense
of the different kinds of initialisations.

Up until now, I thought of an object as either NOT being initialised (i.e.
containing garbage), or being default initialised (i.e. containing the
default value for that type).

Here are some examples of the former:

struct MyStruct {

int a;
double b;
void *p;
};

int main()
{
int a; /* Contains garbage */

double b; /* Contains garbage */

void *p; /* Contains garbage */

MyStruct obj; /* All members contain garbage */
}
And here are some examples of the latter:

struct MyStruct {

int a;
double b;
void *p;
};
int main()
{
int a = int(); /* Contains 0 */

double b = double(); /* Contains 0.0 */

typedef void *voidptr;
void *p = voidptr(); /* Contains null pointer value */

MyStruct obj = {}; /* Each member has the same values as above */
}

The Standard tells me that there are three kinds of initialisation:
Zero-initialisation
Default-initialisation
Value-initialisation
In my above code snippet, what kind of initialisation am I peforming? I
think it's "default initialisation", as I'm giving the object its default
value.

What is zero-initialisation, and can anyone give an example of it please?

What is value-initialisation, and can anyone give an example of it please?
Also, what kind of initialisation does the following class perform?

template<class T>
struct Init {

T obj;

Init() : obj() {}
};

--

Frederick Gotham
Hello Frederick,
I tried your code in VC++ 6.0. It throws compilation
errors.

All the errors point to the code MyStruct obj = {};

\Initialization.cpp(49) : error C2059: syntax error : '}'
\Initialization.cpp(49) : warning C4508: 'main' : function should
return a value; 'void' return type assumed
\Initialization.cpp(53) : error C2143: syntax error : missing ';'
before '}'
\Initialization.cpp(53) : error C2143: syntax error : missing ';'
before '}'
\Initialization.cpp(53) : error C2143: syntax error : missing ';'
before '}'

What compiler are you using?

All,
Is it correct way to initialize the struct like this?

Thanks
Sabiyur

Jul 20 '06 #7
Sabiyur wrote:
Frederick Gotham wrote:
>[..]
Frederick Gotham

Hello Frederick,
I tried your code in VC++ 6.0. [..]
Don't. Throw it away. It's so pre-standard, even it's children are
not compliant.
What compiler are you using?

All,
Is it correct way to initialize the struct like this?
Of course. Get a decent compiler. VC++ 2005 EE is available for
free from MS. No comparison, so much better standard compliance.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 20 '06 #8
Victor Bazarov posted:
>I still don't understand how zero-initialisation is any different to
default-initialisation. . .

For non-POD it involves calling the default c-tor. There are no c-tors
for PODs, and that's why there is no difference between 'default-' and
'zero-' for PODs. Just read the Standard.

I've read the Standard an I don't understand it.

Here's value initialisation:

MyStruct obj = { 1, 5.2, 0 };

Here's default initialisation:

MyStruct obj = {};
So what's zero initialisation?
--

Frederick Gotham
Jul 20 '06 #9
Frederick Gotham wrote:
Victor Bazarov posted:
>>I still don't understand how zero-initialisation is any different to
default-initialisation. . .

For non-POD it involves calling the default c-tor. There are no
c-tors for PODs, and that's why there is no difference between
'default-' and 'zero-' for PODs. Just read the Standard.


I've read the Standard an I don't understand it.

Here's value initialisation:

MyStruct obj = { 1, 5.2, 0 };

Here's default initialisation:

MyStruct obj = {};
So what's zero initialisation?
Zero-initialisation is what happens with the POD when you
default-initialise it. So, the last statement is zero-initialisation
(*by definition* of the default-initialisation for PODs). Which part
of it do you not understand?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 20 '06 #10

"Victor Bazarov" <v.********@comAcast.netwrote
Sabiyur wrote:
... I tried your code in VC++ 6.0 ...

Don't. Throw it away. It's so pre-standard, even it's children are
not compliant.... VC++ 2005 EE is available for free from MS.
No comparison, so much better standard compliance.
I'm using VC++ 6.0 at work. I agree, it's not very compliant. I've
run into several cases where it does unexpected stuff. So I'd like
to upgrade.

HOWEVER.....

When I went to www.microsoft.com and check the pricing, the upgrade
kit from VC++ 6.0 to VC++ 2005 is not listed as free; it's listed
as $550.00. That's if you *qualify* for the upgrade. Otherwise,
the price is $800.

The company I work for is unlikely to agree to spend that kind of
money. (Yah, I know, it's small compared to the savings from
improved productivity; but that's how it is.)

Where did you see the "free" pricing for this product? They have
an "evaluation" version, but that's probably skeletal or
30-day-limited or splash-screen-corrupted. I've got one of
their freebie versions at home, and every program it compiles
displays a big ugly splash screen at startup that says something
about how "this program was compiled with an unlicensed copy
of Visual Studio and is not authorized for public distribution"
or words to that effect. That's useless for anything except as
a toy or teaching aid; can't use it at work.
--
Curious,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
Jul 20 '06 #11

Robbie Hatley wrote:
Where did you see the "free" pricing for this product? They have
an "evaluation" version, but that's probably skeletal or
30-day-limited or splash-screen-corrupted. I've got one of
their freebie versions at home, and every program it compiles
displays a big ugly splash screen at startup that says something
about how "this program was compiled with an unlicensed copy
of Visual Studio and is not authorized for public distribution"
or words to that effect. That's useless for anything except as
a toy or teaching aid; can't use it at work.
Express Edition is free and you can use it for anything I believe. It
doesn't seem to do solutions, has no source control connection,...yeah,
it's missing a few features. It does compile projects though and afaik
it doesn't add a bunch of crap to your exe to flag its use.

If your co. wanted to upgrade they should have gone to the launch. We
all got standard edition and a bunch of other useless crap for sitting
through an 8 hour commercial. At least I was getting paid ;)

Jul 20 '06 #12
Robbie Hatley wrote:
"Victor Bazarov" <v.********@comAcast.netwrote
>Sabiyur wrote:
>>... I tried your code in VC++ 6.0 ...

Don't. Throw it away. It's so pre-standard, even it's children are
not compliant.... VC++ 2005 EE
That's VC++ 2005 *E*xpress *E*dition. Look it up.
is available for free from MS.
>No comparison, so much better standard compliance.

[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 20 '06 #13
Frederick Gotham wrote:
Victor Bazarov posted:

>>What is zero-initialisation, and can anyone give an example of it
please?
That's the incarnation of the default-initialisation for PODs.


The reason we invent words is not so we have a label for something, but so
that we can distinguish it from other things.

That is why I'm confused with "zero-initialsation" Vs. "default-
initialisation".

Are they not exactly the same? If so, why give them different names?
Compare the definitions in the standard which are one above the other.
For a non-POD class type, to default initialize is to call the default
constructor. To zero-initialize the same is to recursively
zero-initialize its base-class subobjects and nonstatic members which
eventually terminates with, essentially, everything being set to 0
(converted as appropriate). Naturally a default ctor could do something
other than set everything to zero, hence the two are not the same.
Jul 21 '06 #14

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

Similar topics

18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
12
by: Yu | last post by:
I have found that the static object is initialised at the time when the shared libary is loaded. The initialisation caused the invocation of the constructor. May I know of any way that I can...
106
by: A | last post by:
Hi, I have always been taught to use an inialization list for initialising data members of a class. I realize that initialsizing primitives and pointers use an inialization list is exactly the...
1
by: Tony Johansson | last post by:
Hello!! Assume I have a wrapper class with this definition. class Integer { public: Integer(int =0); int get() const; Integer& set(int); Integer& add(const Integer&);
11
by: cfchou | last post by:
hi, all, i'm reading ch.20 -smart pointers- of . and i'm tring the trule.hpp test. but there's something different than i expect, and i found that's about temp object. so i simplified the...
10
by: Tom the Canuck | last post by:
What would be the best way to proceed? Should I make a pure virtual class and then derive from that? I want the base class to have functions defined so that I don't have to do the work all over...
2
by: Christoph Conrad | last post by:
Hi, given the following test case, with CString from Microsofts MFC: ================================================== file 1: CString arr; ...
14
by: Jeroen | last post by:
Hi all, I've got a question about writing a library. Let me characterize that library by the following: * there is a class A which is available to the user * there is a class B that is used...
2
by: Brett_McS | last post by:
Fairly new to Python (and loving it!) In C++ (gack!) I got used to creating a helper function with each class to check the class object initialisation parameters prior to creating the object. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...

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.