default initialization of basic types

Pages: 12
Dec 19, 2009 at 7:08pm
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
Im not sure. But try it and output to the screen.
Dec 19, 2009 at 7:17pm
Basic types aren't initialized.
Dec 19, 2009 at 7:28pm
Except in the case of static or globals which are set to 0;
Dec 19, 2009 at 7:42pm
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 8:52pm
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
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
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:22pm
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:28pm
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
The initialization of static variables and globals to 0 is the C++ standard.
Dec 19, 2009 at 10:56pm
still seems like a lousy thing to rely on.

How hard is it to type "= 0;"?
Dec 20, 2009 at 4:02am
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:13am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

struct A{
	int a;
};

int main(){
	{
		A a;
		a.a=10;
	}
	{
		A a;
		std::cout <<a.a<<std::endl;
	}
}

Damn. 1 in 2^32.
Dec 20, 2009 at 4:15pm
Built in types have default initialisers that assign to zero, but they are not called by default.

e.g.
1
2
3
4
int i; // uninitialised
int j = int(); // initialised to zero
int *pi = new int; // uninitialised
int *pj = new int(); // initialised to zero 
Dec 20, 2009 at 4:56pm
You forgot
int i();

Heh heh heh...
Dec 20, 2009 at 4:59pm
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
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
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 6:27pm
It's missing the return type...
Pages: 12