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

Default & Value Initialization

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,

Jun 20 '06 #1
10 8483
utab wrote:
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.

What subject?

Are there any specifics you are unsure of?

--
Ian Collins.
Jun 20 '06 #2

What subject?


intialization(for example:ctors for class types)

In general, I read something but not completely clear to me. (I read
some parts from Accelerated C++.)

Jun 20 '06 #3

utab wrote:
What subject?


intialization(for example:ctors for class types)

In general, I read something but not completely clear to me. (I read
some parts from Accelerated C++.)


I have not read Accelerated C++.
Try reading Effective C++, this book helped me understand
initialization better.

~Madhav

Jun 20 '06 #4
utab wrote:
What subject?

intialization(for example:ctors for class types)

In general, I read something but not completely clear to me. (I read
some parts from Accelerated C++.)

That's a very broad subject for a Usenet posting, are there any specific
areas you'd like clarified?

--
Ian Collins.
Jun 20 '06 #5

That's a very broad subject for a Usenet posting, are there any specific
areas you'd like clarified?


Is there a source that you may direct me for a wider discussion so that
I may spend some time on that myself and then return here if there are
points uncertain to me.

Regards,

Jun 20 '06 #6
utab wrote:
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.


Value initialization was in the 2003 update to the standard, and is
therefore not covered in any books I know of (though surely there is
one). The best source would probably be the draft C++0x standard, here:
http://www.open-std.org/jtc1/sc22/wg...2005/n1905.pdf

See section 8.5.

Tom
Jun 20 '06 #7
utab wrote:
That's a very broad subject for a Usenet posting, are there any specific
areas you'd like clarified?

Is there a source that you may direct me for a wider discussion so that
I may spend some time on that myself and then return here if there are
points uncertain to me.

I can only suggest the book you have and "The C++ Programming Language".

--
Ian Collins.
Jun 20 '06 #8
"utab" <um********@gmail.com> wrote in message
news:11*********************@i40g2000cwc.googlegro ups.com...
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.


Here is a very brief summary:

When a type name or constructor initializer is followed by () [i.e. a pair
of parentheses with only whitespace between them], that calls for value
initialization. The difference between value initialization and the way
local variables are initialized is that value initialization gives an
initial value of zero to members that are out of reach of any constructor.

Here is an example:

struct A {
int x;
int y;
};

struct B {
int x;
std::string s;
};

struct C {
int x;
int y;
C() { }
};

If we write the following:

void foo()
{
A a;
B b;
C c;
}

then the local variables a, b, and c are all default-initialized. That
means that a.x and a.y, b.x, c.x, and c.y all have undefined values. On the
other hand, b.s is initialized to a null string, because that is what the
std::string constructor does.

Now, suppose we do this:

void bar()
{
A a = A();
B b = B();
C c = C(); // undefined behavior!
}

In each case we are value-initializing an otherwise nameless object (by
writing A(), B(), and C() respectively), and copying the value of that
object to the appropriate local variable.

In the case of A(), the x and y members will both be set to zero, because
they are out of reach of any constructor.

In the case of B(), the x member will be set to zero, for exactly the same
reason as the x member of A(); the s member will be initialized to a null
string by its own constructor.

In the case of C(), there is a constructor that is capable of initializing
the x and y members, so no initialization takes place. Attempting to copy
C() to c therefore results in undefined behavior.

The idea behind value initialization is that it takes place when an object
is about to be used in a context that demands a value--that is, when it is
about to be copied. When I write

A a;

there is no request to copy the variable "a", but when I write

A a = A();

there is a request to copy the nameless object represented by A(). For
example, if I had written

int x = int();

this definition would have undefined behavior unless int() had a
well-defined value, so int() is value-initialized to zero.

Incidentally, it would have a totally different meaning if I were to write

A a();

which would declare "a" as a function with no arguments returning an object
of type A.

As for constructor initializers:

struct S {
A a;
S(): a() { }
};

The member initializer "a" is followed by a pair of parentheses, so this
example demands value initialization. In other words, if I write

void foo()
{
S s;
}

then s.a.x and s.a.y will both be initialized to zero because of value
initialization.
Jun 21 '06 #9
Andrew Koenig wrote:
"utab" <um********@gmail.com> wrote in message
news:11*********************@i40g2000cwc.googlegro ups.com...
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.


Here is a very brief summary:

When a type name or constructor initializer is followed by () [i.e. a pair
of parentheses with only whitespace between them], that calls for value
initialization. The difference between value initialization and the way
local variables are initialized is that value initialization gives an
initial value of zero to members that are out of reach of any constructor.

Here is an example:

struct A {
int x;
int y;
};

struct B {
int x;
std::string s;
};

struct C {
int x;
int y;
C() { }
};

If we write the following:

void foo()
{
A a;
B b;
C c;
}

then the local variables a, b, and c are all default-initialized. That
means that a.x and a.y, b.x, c.x, and c.y all have undefined values. On the
other hand, b.s is initialized to a null string, because that is what the
std::string constructor does.


Your terminology seems to differ from what I see in the standard. From
8.5.5, "To default-initialize an object of type T means: [special rules
for non-POD class types and array types]; otherwise, the object is
zero-initialized."

Then in 8.5.9, "If no initializer is specified for an object [special
rules for non-POD class types]. Otherwise, if no initializer is
specified for a non-static object, the object and its subobjects, if
any, have an indeterminate initial value."

In my view, "default initialization" should never leave an object with
undefined values.

-Mark
Jun 21 '06 #10
Mark P wrote:
Andrew Koenig wrote:
Your terminology seems to differ from what I see in the standard. From
8.5.5, "To default-initialize an object of type T means: [special rules
for non-POD class types and array types]; otherwise, the object is
zero-initialized."

Then in 8.5.9, "If no initializer is specified for an object [special
rules for non-POD class types]. Otherwise, if no initializer is
specified for a non-static object, the object and its subobjects, if
any, have an indeterminate initial value."

In my view, "default initialization" should never leave an object with
undefined values.


The paragraphs you quote changed in the 2003 standard:
8.5/5:
To zero-initialize an object of type T means:
— if T is a scalar type (3.9), the object is set to the value of 0
(zero) converted to T;
— if T is a non-union class type, each nonstatic data member and each
base-class subobject is zeroinitialized;
— if T is a union type, the object’s first named data member89) is
zero-initialized;
— if T is an array type, each element is zero-initialized;
— if T is a reference type, no initialization is performed.

To default-initialize an object of type T means:
— if T is a non-POD class type (clause 9), the default constructor for T
is called (and the initialization is ill-formed if T has no accessible
default constructor);
— if T is an array type, each element is default-initialized;
— otherwise, the object is zero-initialized.

To value-initialize an object of type T means:
— if T is a class type (clause 9) with a user-declared constructor
(12.1), then the default constructor for T is
called (and the initialization is ill-formed if T has no accessible
default constructor);
— if T is a non-union class type without a user-declared constructor,
then every non-static data member
and base-class component of T is value-initialized;
— if T is an array type, then each element is value-initialized;
— otherwise, the object is zero-initialized.

Tom
Jun 22 '06 #11

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

Similar topics

4
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...
10
by: JKop | last post by:
What's the difference between them? Take the following: #include <iostream> struct Blah { int k;
9
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....
4
by: Rein Anders Apeland | last post by:
Consider the following working code: #include <memory> #include <iostream> void print_ptr( const std::auto_ptr< int > & thePtr = std::auto_ptr< int >() ) {
4
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: '...
74
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...
23
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...
4
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...
7
by: Scott Stark | last post by:
Hello, I've got some code working but I'm not sure that the way I implemented it is the best way. I have a custom collection class with a boolean property HasChanged that tells me if any of the...
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
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...
0
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...
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.