Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   what's the difference between value-initialization and default-initialization? (http://www.velocityreviews.com/forums/t501820-whats-the-difference-between-value-initialization-and-default-initialization.html)

Jess 04-25-2007 05:11 AM

what's the difference between value-initialization and default-initialization?
 
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?

Thanks.


Gianni Mariani 04-25-2007 06:36 AM

Re: what's the difference between value-initialization and default-initialization?
 
Jess wrote:
> 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,


Not only built-in types but POD (plain old data) types.

> the value is usually garbage. Is this right?


Yes, unless you use a () in your initialization of the POD members/data.

>
> However, I'm a bit confused about value-initialization, when does it
> happen, and what kind of values are assigned to objects?


You assign whatever value type you enable. Usually, a class has a copy
constructor and an assignment operator available.

i.e.

struct Z
{
Z() {} // YYY
Z( const char * ) {}
Z & operator=( const char * ) {} // XXX
};

Z x( "HI" );

Z y( x ); // copy constructor (compiler provided YYY)

int main()
{

y = x; // assignment operator (compiler provided)

y = "THERE"; // assignment operator XXX

}

Jess 04-26-2007 04:19 AM

Re: what's the difference between value-initialization and default-initialization?
 
> Not only built-in types but POD (plain old data) types.

What is POD?

> Yes, unless you use a () in your initialization of the POD members/data.

Again, could you please give more explanation?

> You assign whatever value type you enable. Usually, a class has a copy
> constructor and an assignment operator available.
>
> i.e.
>
> struct Z
> {
> Z() {} // YYY
> Z( const char * ) {}
> Z & operator=( const char * ) {} // XXX
>
> };
>
> Z x( "HI" );
>
> Z y( x ); // copy constructor (compiler provided YYY)
>
> int main()
> {
>
> y = x; // assignment operator (compiler provided)
>
> y = "THERE"; // assignment operator XXX
>
> }

It'd be very helpful if you could explain the example. Thanks a lot!


Mumia W. 04-26-2007 05:43 AM

Re: what's the difference between value-initialization and default-initialization?
 
On 04/25/2007 12:11 AM, Jess wrote:
> 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?
>
> Thanks.
>



#include <cstdio>
#include <cstdlib>

class Foo {
public:
int value;
const char * str;

void print (FILE * fh) {
fprintf(fh, "%d => %s\n", value, str);
}
};

int main ()
{
Foo myfoo = { 10, "Hello" };
myfoo.print(stdout);

return EXIT_SUCCESS;
}

-----end-code--------

Notice that myfoo was initialized by values written in the style used to
initialize POD (plain old data--known in C as 'structs').


Gianni Mariani 04-26-2007 09:13 AM

Re: what's the difference between value-initialization and default-initialization?
 
Jess wrote:
> > Not only built-in types but POD (plain old data) types.

>
> What is POD?


Google "POD (plain old data)" - here is an "I'm Feeling Lucky Link"

http://www.google.com/search?hl=en&q...=Google+Search

>
>> Yes, unless you use a () in your initialization of the POD members/data.

> Again, could you please give more explanation?



Consider this:

struct A { int x; A() : x() {} };

struct B { int x; };

int main()
{
A a;
B b;

return a.x == b.x;
}

The value of a.x is always 0. The value of b.x is indeterminate or
uninitialized.

This applies for all POD types.

B is a POD type;

consider:
B b( B() );

.... b.x is now initialized.

Note that if you had written:
B b(); // function declaration of function b() returning a B.
.... nasty little issue for newbies...


>
>> You assign whatever value type you enable. Usually, a class has a copy
>> constructor and an assignment operator available.
>>
>> i.e.
>>
>> struct Z
>> {
>> Z() {} // YYY
>> Z( const char * ) {}
>> Z & operator=( const char * ) {} // XXX
>>
>> };
>>
>> Z x( "HI" );
>>
>> Z y( x ); // copy constructor (compiler provided YYY)
>>
>> int main()
>> {
>>
>> y = x; // assignment operator (compiler provided)
>>
>> y = "THERE"; // assignment operator XXX
>>
>> }

> It'd be very helpful if you could explain the example. Thanks a lot!


You need to read the chapter on constructors in C++ in your favorite
book and come back.

James Kanze 04-26-2007 11:23 AM

Re: what's the difference between value-initialization and default-initialization?
 
On Apr 25, 7:11 am, Jess <w...@hotmail.com> wrote:

> 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?


Written with a hyphen, no. "default-initialization" is a
technical term, defined and used in the standard:

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.

Note, however, that variables aren't default-initialized by
default:-).

> However, I'm a bit confused about value-initialization, when
> does it happen, and what kind of values are assigned to
> objects?


Again, a technical term used in the standard (or at least the
latest draft). I'll admit that when reading the standard, I
can't quite make out what the intended difference between
default initialization and value initialization is supposed to
be. The original version (ISO 14882:1998) doesn't have value
initialization.

I suspect that the intent is to ensure that zero initialization
occurs when the type has a non trivial implicit constructor.
The exact wording is:

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 baseclass component of T is
value-initialized;

-- if T is an array type, then each element is
value-initialized;

-- otherwise, the object is zero-initialized

Basically, what gets added is the second point. If I have a
class something like:

class C
{
public:
virtual ~C() {}
int x ;
} ;

It's not a POD, but it doesn't have a user defined
constructor, so value-initialization guarantees that x is
set to 0, whereas default-initialization doesn't.

And to quote the standard: "An object whose initializer is
an empty set of parentheses, i.e. () shall be
value-initialized." (In the latest draft;
default-initialized in the 1998 version of the standard.)

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


Ron Natalie 04-26-2007 11:36 AM

Re: what's the difference between value-initialization and default-initialization?
 
James Kanze wrote:

>
> Note, however, that variables aren't default-initialized by
> default:-).


One of the most stupid features of C++. Variables aren't always
default-initialized.

>
> I suspect that the intent is to ensure that zero initialization
> occurs when the type has a non trivial implicit constructor.
> The exact wording is:
>

Well, more specifically, it makes the behavior consistent when
the object contains objects of possibly non-POD type. Before
this concept was defined you couldn't figure out how:

struct S {
int i;
T t;
} s;

would be initialized unless you knew what T was.

Jess 04-29-2007 10:17 AM

Re: what's the difference between value-initialization and default-initialization?
 
On Apr 26, 7:13 pm, Gianni Mariani <gi3nos...@mariani.ws> wrote:
> struct A { int x; A() : x() {} };
>
> struct B { int x; };
>
> int main()
> {
> A a;
> B b;
>
> return a.x == b.x;
>
> }
>
> The value of a.x is always 0. The value of b.x is indeterminate or
> uninitialized.


This is called default-initialization, right?

> consider:
> B b( B() );
>
> ... b.x is now initialized.


I don't quite understand this... "B()" creates a temporary object and
it's passed to a copy-constructor to initialize "b", and the copy
constructor tries to copy "x" value of the tmp object to "b"'s "x".
However, what is the value of tmp object's "x"?

> You need to read the chapter on constructors in C++ in your favorite
> book and come back.


I did read Accelerated C++, but am still confused. In order to
clarify the matter, here's what I think, not sure whether they are
right/wrong:

1. Default initialization is used when we create an object using the
form "T t", where T can be a POD type or class type. If this happens,
the following occurs:
a. if T is POD, then "t" is garbage value
b. if T is class, then it's default constructor is invoked. If
there's no default contructor, the compiler will synthesize one.
(i). If there's a default constructor, then all T's POD member data is
initialized according to the constructor initializer list; any POD
member not mentioned in the initializer list gets default-init, with
garbage value. If T has a class-typed member (say of type A), then
A's default constructor is called and if A has no default constructor,
then the synthesized constructor will be called. FInally, the
constructor's body is entered.
(ii). If there is no default constructor, then the synthesized
constructor will be called. All POD member gets garbage data and
class members have their default constructor (or synthesized default
constructors) invoked.

Is this what happens to default-init? Moreover, to init POD member in
the constructor initializer, I can do things like "x(10)" and also
"x()" where "x" is an int, is this right? If "x()" is used, then "x"
gets value-init to 0?

2. Value-initialization happens if a constructor is explicitly invoked
like "T t(arg)", or when we invoke copy constructor. For a class type
object:
a. if it's created by invoking a constructor like "T t(arg)", then all
its POD member gets initialized according to the constructor
initializer; any POD member not mentioned gets value-init to 0. But
what happens to those class-typed members? Are their default
constructors (or synthesized one) called? I think after all these are
done, the constructor's body is entered.
b. if the T object is created through copy constructor, then the
values of the existing objects are copied into the new object.
However, what if we have an example like "B b(B())"? what's the value
of the tmp object?

3. To value-init a POD object, I think the only way is to do something
like:

int n = int();
is this right?

4. The book Accelerated C++ also says that if an object is used to
init a container element, either as a side effect of adding new
element to a "map", or as the elements of a container defined to have
a given size, then the members will be value-initialized. I checked
it through an example,

struct A{
int x;
};

int main(){
A a[10];
return 0;
};

Then I found out each element in a has it's "x" value as 0. I think
this is what the author of the book meant. Then I tried another
example:

int main(){
int b[10];
return 0;
}

This time, each element in b has garbage value. Isn't each member of
b supposed to be value-init to 0?

I'd really appreciate it if some of you could answer my questions and
point out my mistakes and also let me know the points I overlooked.
Thanks.
Jess



Victor Bazarov 04-29-2007 04:13 PM

Re: what's the difference between value-initialization and default-initialization?
 
Jess wrote:
> On Apr 26, 7:13 pm, Gianni Mariani <gi3nos...@mariani.ws> wrote:
>> consider:
>> B b( B() );
>>
>> ... b.x is now initialized.

>
> I don't quite understand this... "B()" creates a temporary object [..]


B b(B());

is a declaration of a funciton. There is no b.x. 'b' is a function.

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



Jess 05-01-2007 12:43 AM

Re: what's the difference between value-initialization and default-initialization?
 
On Apr 30, 2:13 am, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> B b(B());
>
> is a declaration of a funciton. There is no b.x. 'b' is a function.


But in your previous message, you mentioned "b.x" is initialized?
Also, "B()" calls B's default constructor, and the result is a B
object. When we use this temporary object to "B b(B())", aren't we
calling the copy-constructor?

Thanks
Jess



All times are GMT. The time now is 09:28 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.