Dec 19, 2009 at 7:08pm Dec 19, 2009 at 7:08pm UTC
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
Dec 19, 2009 at 7:13pm Dec 19, 2009 at 7:13pm UTC
Im not sure. But try it and output to the screen.
Dec 19, 2009 at 7:17pm Dec 19, 2009 at 7:17pm UTC
Basic types aren't initialized.
Dec 19, 2009 at 7:28pm Dec 19, 2009 at 7:28pm UTC
Except in the case of static or globals which are set to 0;
Dec 19, 2009 at 7:42pm Dec 19, 2009 at 7:42pm UTC
Also class members, which are initialized to zero by default, as well.
Last edited on Dec 19, 2009 at 7:42pm Dec 19, 2009 at 7:42pm UTC
Dec 19, 2009 at 8:52pm Dec 19, 2009 at 8:52pm UTC
o_O
Is that true?
I was under the impression that nothing was initialized to zero unless you did so explicitly.
Dec 19, 2009 at 9:10pm Dec 19, 2009 at 9:10pm UTC
It's because static data such as globals are always constructed, and the default constructors of members are called if no other constructor is implicitly called in the initialization list. The default constructors of basic types initialize to zero.
Dec 19, 2009 at 10:18pm Dec 19, 2009 at 10:18pm UTC
I'm skeptical. I know that if you put empty parenthesis on a basic data type it's zeroed, but if you don't specify at all, it's not initialized.
I just tested it with GCC to confirm:
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
#include <iostream>
using namespace std;
class Foo
{
public :
int v;
};
class Foo2
{
public :
Foo2() { }
int v;
};
int main()
{
Foo a;
Foo2 b;
cout << a.v << endl;
cout << b.v << endl;
char c;
cin >> c;
return 0;
}
As I expected, both couts output garbage (not zero).
I did test the static, though, and that was zeroed, but I'm still skeptical as to whether or not that's really reliable.
EDIT: Tried a global and that was zeroed too -- but again... I'm weary.
Last edited on Dec 19, 2009 at 10:20pm Dec 19, 2009 at 10:20pm UTC
Dec 19, 2009 at 10:22pm Dec 19, 2009 at 10:22pm UTC
I tested it before posting at 4:42.
1 2 3 4 5 6 7 8 9 10
#include <iostream>
struct A{
int a;
};
int main(){
A a;
std::cout <<a.a<<std::endl;
}
Although I would never rely on a value being initialized.
Last edited on Dec 19, 2009 at 10:24pm Dec 19, 2009 at 10:24pm UTC
Dec 19, 2009 at 10:28pm Dec 19, 2009 at 10:28pm UTC
I just tested your code and got garbage output.
I'm looking up my compiler version now, but I forget where it is exactly....
Dec 19, 2009 at 10:52pm Dec 19, 2009 at 10:52pm UTC
The initialization of static variables and globals to 0 is the C++ standard.
Dec 19, 2009 at 10:56pm Dec 19, 2009 at 10:56pm UTC
still seems like a lousy thing to rely on.
How hard is it to type "= 0;"?
Dec 20, 2009 at 4:02am Dec 20, 2009 at 4:02am UTC
I am absolutely, positively, 110% sure that helios is wrong. (sorry).
What helios saw was that memory just happened to be zero.
Dec 20, 2009 at 4:56pm Dec 20, 2009 at 4:56pm UTC
You forgot
int i();
Heh heh heh...
Dec 20, 2009 at 4:59pm Dec 20, 2009 at 4:59pm UTC
What would be the point in
int j = int (); // initialised to zero
You may as well explicitly initialise that to 0, it would be clearer and save a whole 4 bytes.
Dec 20, 2009 at 5:03pm Dec 20, 2009 at 5:03pm UTC
The point is when you're dealing with data where the type is unknown, but you need correct behavior; templates. Without templates, none of this is necessary.
Dec 20, 2009 at 6:16pm Dec 20, 2009 at 6:16pm UTC
Oh, you're talking about generic programming. My apologies. All I can do with templates is this:
1 2 3 4 5
template <typename T>
T myFunction(T param) {
std::cout << param;
return param;
}
and I'm not even sure that's right...
Last edited on Dec 20, 2009 at 7:32pm Dec 20, 2009 at 7:32pm UTC
Dec 20, 2009 at 6:27pm Dec 20, 2009 at 6:27pm UTC
It's missing the return type...