Go Back   Velocity Reviews > Newsgroups > C++
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C++ - "Default constructor" for built-in types

 
Thread Tools Search this Thread
Old 01-25-2005, 04:56 PM   #1
Default "Default constructor" for built-in types


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?

Thanks,

--
Pierre Senellart


Pierre Senellart
  Reply With Quote
Old 01-26-2005, 04:59 PM   #2
Dietmar Kuehl
 
Posts: n/a
Default Re: "Default constructor" for built-in types

Pierre Senellart wrote:
> > 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?


Yes. Effectively, it means that for POD types (which includes the
built-in types) the values are zero initialized.
--
<private.php?do=newpm&u=> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting



Dietmar Kuehl
  Reply With Quote
Old 01-26-2005, 11:18 PM   #3
Mark
 
Posts: n/a
Default Re: "Default constructor" for built-in types
>
> Yes. Effectively, it means that for POD types (which includes the
> built-in types) the values are zero initialized.


I'm confused. I ran a test. When compiling a .cpp file, my compiler
warns about uninitialised variables, and the values assigned to
them are random which is shown when I print them out.

I'm using MSVC++ 6.0

Although MSVC++ 6.0 has STL does anyone know if it breaks
the standard in this respect. It seems such a fundemental rule,
then I would expect it not to. But then again, see above.


Mark







Mark
  Reply With Quote
Old 01-26-2005, 11:23 PM   #4
Victor Bazarov
 
Posts: n/a
Default Re: "Default constructor" for built-in types
Mark wrote:
>>Yes. Effectively, it means that for POD types (which includes the
>>built-in types) the values are zero initialized.

>
>
> I'm confused. I ran a test. When compiling a .cpp file,


Care to share the file with us?

> my compiler
> warns about uninitialised variables, and the values assigned to
> them are random which is shown when I print them out.
>
> [...]



Victor Bazarov
  Reply With Quote
Old 01-27-2005, 12:55 AM   #5
Mark
 
Posts: n/a
Default Re: "Default constructor" for built-in types

>
> Care to share the file with us?
>


something as simple as:

#include <iostream>
using namespace std;

unsigned int main(unsigned int argc,
char* argv[])
{
unsigned int i;

cout << i << endl;

return 0;
}

I get
warning:local variable 'i' is used without being initialized.
The program outputs what ever value was in memory at the
time.


Mark




Mark
  Reply With Quote
Old 01-27-2005, 02:56 AM   #6
Victor Bazarov
 
Posts: n/a
Default Re: "Default constructor" for built-in types
"Mark" <> wrote...
>
>>
>> Care to share the file with us?
>>

>
> something as simple as:
>
> #include <iostream>
> using namespace std;
>
> unsigned int main(unsigned int argc,
> char* argv[])
> {
> unsigned int i;
>
> cout << i << endl;
>
> return 0;
> }
>
> I get
> warning:local variable 'i' is used without being initialized.
> The program outputs what ever value was in memory at the
> time.


Yes. Why are you surprised? To initialise the variable you need
to write

unsigned int i(42); // or whatever value you prefer

V




Victor Bazarov
  Reply With Quote
Old 01-27-2005, 04:13 AM   #7
Mark
 
Posts: n/a
Default Re: "Default constructor" for built-in types
>
> Yes. Why are you surprised? To initialise the variable you need
> to write
>
> unsigned int i(42); // or whatever value you prefer
>


_because in the context of the previous post_,

> Does that mean that, on built-in types (e.g. int), "default
> initialization" (as described in 8.5) is performed?


Yes. Effectively, it means that for POD types (which includes the
built-in types) the values are zero initialized.

I was expecting

unsigned int i;

to have a default initialiser of zero.

Basically I just need to clear up my understanding of the standard
behaviour, and work out where my misunderstanding lies.
In practice , I always endeavour to explicitly initialise my vars
re: best practice, so it would'nt normally be a problem.

Mark




Mark
  Reply With Quote
Old 01-27-2005, 06:05 AM   #8
Victor Bazarov
 
Posts: n/a
Default Re: "Default constructor" for built-in types
"Mark" <> wrote...
> >
>> Yes. Why are you surprised? To initialise the variable you need
>> to write
>>
>> unsigned int i(42); // or whatever value you prefer
>>

>
> _because in the context of the previous post_,
>
>> Does that mean that, on built-in types (e.g. int), "default
>> initialization" (as described in 8.5) is performed?

>
> Yes. Effectively, it means that for POD types (which includes the
> built-in types) the values are zero initialized.


In the 'valarray's constructor, yes. Just like in the 'vector's
constructor, for example.

> I was expecting
>
> unsigned int i;
>
> to have a default initialiser of zero.


WHY? The 'i' variable here is not _default_-initialised. It is
_uninitialised_. In the statement above there is no _initialiser_.
Read 8.5/9, it should be clear enough.

> Basically I just need to clear up my understanding of the standard
> behaviour, and work out where my misunderstanding lies.


Do you have a copy of the Standard? Then read 8.5 carefully. If
you don't have a copy of the Standard, how do you expect to clear
up your understanding of it?

> In practice , I always endeavour to explicitly initialise my vars
> re: best practice, so it would'nt normally be a problem.


That's called "avoiding the issue".

V




Victor Bazarov
  Reply With Quote
Old 01-27-2005, 08:53 AM   #9
Dietmar Kuehl
 
Posts: n/a
Default Re: "Default constructor" for built-in types
Mark wrote:
> #include <iostream>
> using namespace std;
>
> unsigned int main(unsigned int argc,


BTW, the return type of 'main()' is 'int' according to
the standard, not 'unsigned int'. The same applies to
the first argument.

> char* argv[])
> {
> unsigned int i;
>
> cout << i << endl;
>
> return 0;
> }


I don't see any mention of 'valarray' in this file. Also,
I don't see any trace of default initializing a built-in
value. Default initialization is typically requested by
tagging a pair of parenthesis on the appropriate type or
member. For example:

/**/ template <typename T>
/**/ struct foo {
/**/ foo():
/**/ member() // <--- this is default initialization
/**/ {}
/**/ T member;
/**/ };

You can try it out: if you instantiated this class with an
'unsigned int' (or some other built-in type) it should always
be zero. You can also verify that without the default
initialization the value of 'member' is not necessarily zero
(although there is no guarantee that it is not initialized)
if you remove the mention of 'member' in the member
initializer list. Here is a small program displaying this:

/**/ #include <new>
/**/ #include <iostream>
/**/ int main() {
/**/ void* mem = operator new(sizeof(foo<int>));
/**/ foo<int>* obj = new(mem) foo<int>;
/**/ std::cout << obj->member << "\n";
/**/ obj->member = 17;
/**/ obj->~foo<int>();
/**/ obj = new(mem) foo<int>;
/**/ std::cout << obj->member << "\n";
/**/ obj->~foo<int>();
/**/ operator delete(mem);
/**/ }

For members with class types the default initialization is kind
of redundant because it would happen even if the member was not
mentioned in the member initializer list. For members with
built-in, POD, or aggregate types it makes a difference though:
these stay uninitialized (for aggregates only members of POD
or built-in types would be uninitialized; aggregate members
with class type are, of course, default constructed).
--
<private.php?do=newpm&u=> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting



Dietmar Kuehl
  Reply With Quote
Old 01-27-2005, 12:37 PM   #10
Ron Natalie
 
Posts: n/a
Default Re: "Default constructor" for built-in types
There's no "default" constructor for non-class types, but there
is default (zero) initialization. Unfortunately, for braindead
compatibility with C, the default initialization is NOT done for
POD types in the following circumstances:

Naked (i.e., declared without initializers) variables local to
a class or function.

dynamically allocated instances.

However, in other places (notably static variables) and in the
case of anything given the empty initializer paramters (when
that is valid), gets the default (zero) initialization.


Ron Natalie
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46