-
December 19th, 2009, 02:07 PM
#1
default initialization of basic types in c++
What is the default initialization of basic data types such as int, char, pointer, array in different declarations such as when declared on function stack, in class, function static, class static, global, on thread stack, etc. ?
thanks,
Sam
-
December 19th, 2009, 03:12 PM
#2
Re: default initialization of basic types in c++
There is no default initialization on basic (POD) types. Only in classes via constructor.
-
December 19th, 2009, 03:33 PM
#3
Re: default initialization of basic types in c++
Originally Posted by hoxsiew
There is no default initialization on basic (POD) types. Only in classes via constructor.
Consider this class.
Code:
class A {
public:
int d1;
char d2;
float d3;
int *d4;
};
int main() {
A a;
cout << a.d1 << " " << (int)a.d2 << " " << a.d3 << " " << a.d4;
return 0;
}
The default constructor should initialize the basic data types but it doesn't. This prints random values, e.g.
2281060 -24 1.63647e+20 0x6116b71a
Last edited by samitj; December 19th, 2009 at 03:33 PM.
Reason: code
-
December 19th, 2009, 03:44 PM
#4
Re: default initialization of basic types in c++
Originally Posted by samitj
The default constructor should initialize the basic data types...
Where did you get this idea?
-
December 19th, 2009, 08:17 PM
#5
Re: default initialization of basic types in c++
No, the constructor does nothing of the sort. C/C++ is designed to be FAST and do exactly what you tell it to do. If you don't tell it to do something, then it doesn't. Are you coming from a java / basic / C# background? In those languages, numbers initialize to zero, not in C++, the memory is just allocated, so whatever was stored at that address previous is what is stored in the variable.
You can create a constructor that initializes those values if you want, but it doesn't happen automatically.
-
December 20th, 2009, 02:39 AM
#6
Re: default initialization of basic types in c++
Originally Posted by samitj
The default constructor should initialize the basic data types but it doesn't. This prints random values, e.g.
2281060 -24 1.63647e+20 0x6116b71a
that's because object a is defined inside main,
any variables defined inside the function body is uninitialized,
class members of built-in types and compound types obey the same the rule,
and are only default initialized for the object that are defined outside the function body.
Code:
class A {
public:
int d1;
char d2;
float d3;
int *d4;
};
A a; // now global
int main() {
cout << a.d1 << " " << (int)a.d2 << " " << a.d3 << " " << a.d4;
return 0;
}
all the members of class A are default intiailized.
Originally Posted by hoxsiew
There is no default initialization on basic (POD) types. Only in classes via constructor.
Yes there is.
Originally Posted by ninja9578
No, the constructor does nothing of the sort.
Does nothing of what sort? You surely don't mean member initialization now, do you?
Edit:
Originally Posted by ninja9578
Are you coming from a java / basic / C# background?
are you yourself coming from long years of C? takin your confidence gained over years
and just did a study on the difference between the two?
If so, you might want to consider learning C++ the right way from the start.
(which wouldn't be too difficult for you anyway)
I don't mean this in any degrading way, so please don't take it the wrong way.
Last edited by potatoCode; December 20th, 2009 at 03:11 AM.
-
December 20th, 2009, 03:23 AM
#7
Re: default initialization of basic types in c++
Originally Posted by potatoCode
that's because object a is defined inside main,
any variables defined inside the function body is uninitialized,
class members of built-in types and compound types obey the same the rule,
and are only default initialized for the object that are defined outside the function body.
More accurately, if no initialiser is provided for an object of a POD type without static storage duration, then the object has (and its members, if any, have) an indeterminate initial value.
Originally Posted by potatoCode
all the members of class A are default intiailized.
I think that it would be more accurate to say that they are zero initialised, although default initialisation for objects of built-in and pointer types is zero initialisation.
-
December 20th, 2009, 03:29 AM
#8
Re: default initialization of basic types in c++
How are you laserlight?
You explain it 10 times better than me (that's probably you know that much more )
But watch out buddy
I'll be "tailgating" you like you're caught in the worst traffic!
-
December 20th, 2009, 05:55 AM
#9
Re: default initialization of basic types in c++
Furthermore, it's worth noting that the upcoming C++ standard defines the new "value-initialization" concept and that ( with the new initializer lists feature ) there will be a uniform expression for value initializing any object of any default constructible type: "T x = {};"
-
December 20th, 2009, 10:29 AM
#10
Re: default initialization of basic types in c++
Originally Posted by potatoCode
Does nothing of what sort? You surely don't mean member initialization now, do you?
Of course, this thread is about the basic types, constructors initialize classes, which aren't basic types.
are you yourself coming from long years of C? takin your confidence gained over years
and just did a study on the difference between the two?
If so, you might want to consider learning C++ the right way from the start.
(which wouldn't be too difficult for you anyway)
I don't mean this in any degrading way, so please don't take it the wrong way.
Yes, and I've been studying the difference between the two and gotten much better at doing things in a C++-way. I was just asking why the poster thought that basic types get initialized, I know that the three languages that I mention do that for him. Taken that I know all three, I would be able to help him know the major differences between them.
-
December 21st, 2009, 07:04 PM
#11
Re: default initialization of basic types in c++
Here is an example to supplement what laserlight and hoxsiew were pointing out. Default/zero initialization of basic types occcurs when you use the constructor initializer. I think that this is a misunderstood concept. Most people would probably just put the zero in the parens just to be sure but according to the std that isn't necessary if you do it like this.
Code:
class A {
public:
A() : d1(), d2(), d3(), d4()
{
}
int d1;
char d2;
float d3;
int *d4;
};
A a; // now global
int main() {
std::cout << a.d1 << " " << (int)a.d2 << " " << a.d3 << " " << a.d4 << std::endl;
return 0;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|