473,545 Members | 2,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"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
Jul 23 '05 #1
9 3706

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.
--
<mailto:di***** ******@yahoo.co m> <http://www.dietmar-kuehl.de/>
<http://www.contendix.c om> - Software Development & Consulting

Jul 23 '05 #2
>
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

Jul 23 '05 #3
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.

[...]

Jul 23 '05 #4

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
Jul 23 '05 #5
"Mark" <ms******@REMOV ETHISBITbtinter net.com> 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
Jul 23 '05 #6
>
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 misunderstandin g lies.
In practice , I always endeavour to explicitly initialise my vars
re: best practice, so it would'nt normally be a problem.

Mark
Jul 23 '05 #7
"Mark" <ms******@REMOV ETHISBITbtinter net.com> 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 misunderstandin g 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
Jul 23 '05 #8
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).
--
<mailto:di***** ******@yahoo.co m> <http://www.dietmar-kuehl.de/>
<http://www.contendix.c om> - Software Development & Consulting

Jul 23 '05 #9
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.
Jul 23 '05 #10

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

Similar topics

3
5562
by: C# Learner | last post by:
Is it possible to set a command button to the "default" button of the form? Here's an example of what I mean -- press Start -> Run. Notice that the "OK" button has a dark rectangle around it, and when you press ENTER, this "OK" button's Click event handler is eventually fired. I was expecting Button to have a Default property, but can't...
3
5722
by: hazz | last post by:
Is Activator.CreateInstance(t,BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.CreateInstance, null, args, null) appropriate given the following; ************************************** public interface IPasswordProvider { string GetPassword(Token token); } public DBPassword()
1
1917
by: Brian | last post by:
Is there a way to change the default "using" statements that are created by Visual Studio when you create a new class? Do I have to create a new file type? So, instead of choosing "New -> Class" I would have to choose "New -> MyClass". Basically, I want to add "Using System.IO;" and "Using System.Diagnostics" so that when I create a new...
2
1492
by: ABC | last post by:
How to return the full virtual path if I pass the url as "~/default.aspx"? I expected this should result as http://aaa.bbb.com/default.aspx.
1
1771
by: Jeff Brown | last post by:
Hi, I'm trying to implement the functionality of the generic expression, "default(T)", but for a given instance of the Type class. The ideal would be if the Type class had a method like "object Type.MakeDefaultInstance()" -- but it doesn't. Here's my attempt, but it seems rather kludgy. Is there a better way?
9
2058
by: Joseph Turian | last post by:
Consider this code snippet which doesn't compile: struct DebugOptions { }; class Debug { public: Debug(const DebugOptions options) { _options = options; } private:
0
1122
by: John Paul. A | last post by:
Hi, I want to get the port number of "Default Web Site" from IIS. Can you please tell me how to get it using the namespace System.DirectoryServices. Thanks, John
3
2888
by: per9000 | last post by:
Hi, can I print some default text on the console when doing a ReadLine? A silly example below shows two small scenarios. In the first a user is told what he appears to be called, and asks for a name. In the second case I want the the user to be able to just press return to accept the username of the system. (I understand it does not work...
2
4677
by: TheSteph | last post by:
Hi, does anybody know how I can get the "default system Border Width " ? example : if I draw a TextBox with the default 3D border, what is the with (thikness) of the border ? Thanks for your help ! Steph3.
1
9611
by: Simon van Beek | last post by:
Dear reader, Under | Tools | Options | the form Options will open. Under the tab you find the field "Default database folder". By opening the form this field shows on my pc I:\. I can't close the form because then the message pups up "MS Office Access can't change the work directory to I:\ " If I change it to D:\ I can close...
0
7479
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7439
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...
0
7773
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...
1
5343
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...
0
3468
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...
0
3450
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1901
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
1
1028
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
722
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.