default initialization of basic types

Pages: 12
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
Im not sure. But try it and output to the screen.
Basic types aren't initialized.
Except in the case of static or globals which are set to 0;
Also class members, which are initialized to zero by default, as well.
Last edited on
o_O

Is that true?

I was under the impression that nothing was initialized to zero unless you did so explicitly.
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.
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
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
I just tested your code and got garbage output.

I'm looking up my compiler version now, but I forget where it is exactly....
The initialization of static variables and globals to 0 is the C++ standard.
still seems like a lousy thing to rely on.

How hard is it to type "= 0;"?
I am absolutely, positively, 110% sure that helios is wrong. (sorry).

What helios saw was that memory just happened to be zero.
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.
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 
You forgot
int i();

Heh heh heh...
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.
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.
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
It's missing the return type...
Pages: 12