472,373 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,373 software developers and data experts.

"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 3595

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.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - 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******@REMOVETHISBITbtinternet.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 misunderstanding 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******@REMOVETHISBITbtinternet.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 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
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.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - 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
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,...
3
by: hazz | last post by:
Is Activator.CreateInstance(t,BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.CreateInstance, null, args, null) appropriate given the following; ...
1
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"...
2
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
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...
9
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
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
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...
2
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...
1
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...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

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.