473,883 Members | 1,686 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 3726

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
5580
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 seem to find anything like it...
3
5752
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
1929
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 class, they will automatically show up. VS Macro?? Help..
2
1510
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
1783
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
2081
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
1134
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
2912
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 the way I do it below - I just want to illustrate what I want).
2
4698
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
9638
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 the form but by opening the form again the field is still showing I:\.
0
9792
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10742
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10847
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10415
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7971
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7126
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5991
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4220
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3232
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.