I am newbie to C++ and pretty confused about initialization of local variables for built-in and class types.

Here's a snippet of the code:

1 #include <iostream>
2 using namespace std;
3
4 class A {
5
6 public :
7 A() : x(9) {};
8 int x;
9
10 };
11
12 A global_a;
13 int global_b;
14
15 int main() {
16
17 A local_a;
18 int local_b;
19 cout << "global_a.x = " << global_a.x << '\n';
20 cout << "local_a.x = " << local_a.x << '\n';
21
22 cout << "global_b = " << global_b << '\n';
23 cout << "local_b = " << local_b << '\n';
24
25 }

Results using my g++ compiler on ubuntu linux:

global_a.x = 9

local_a.x = 9

global_b = 0

local_b = 0

I do think local_b should be undefined but somehow compiler initialized it by default. However local_a .. i am not sure if that should be initialized by default. From the testing here .. local_a seem to be initialized. Not sure if that complies with standard c++ specification (eg C++ PRimer 4th edition says default constructor is used regardless where a class variable is declared - does that means variable of class type is initialized whether it is global or local?).

I dont think the c++ standard sets a hard and fast way an implimentation must initilise built in types but i think compilers generally initilise to 0 to be kind to us.

With things like vector and string they are always initilised as empty so i think they just decided well we do it for those initilise everything to 0 or empty whatever is appropriate. Its the kind of thing it may get initilised to 0 when its a local variable but do NOT expect any initilisation if you get a variable from new or using an allocator or malloc.

Hope this helps cause its as much as i can tell you to be honest sorry

globals are initialized to 0. Locals are left uninitialized, so they just contain whatever random value was at that memory location when the variable is created. If you see a local that has the value of 0 then that means either you explicitly initialized it to 0 or the value of the memory location was already 0. Allocated memory return by either new or malloc() works the same way.

Why then local_a.x has value of 9?

I already told you -- read this VERY VERY SLOWLY: The default value of local variables is what was already contained in the memory location. If it has a value of 9 then that means that memory location contained a 9. Run the program again and it might display a different value.

I put 8 and it print 8. I put 7 and it print 7.

If you set the variable to 8 then it had better print 8. Afterall, that's what you told it to do.

Member Avatar for embooglement

It's displaying a 9 because you're initializing the value x to 9. You told it to be 9. Or 8. Or whatever number you put in this part "A() : x(9) {};" x(some_number) tells the compiler to initialize the variable x to some_number.

C++ Primer 4th edition says default constructor is used regardless where a class variable is declared - does that means variable of class type is initialized whether it is global or local?

Yes, that's what it means, as long as the class has one or more constructors.

Even if the class has no explicitly defined constructor, but has a member with a type that has a constructor, then that constructor is used to initialize that member. In addition, a variable with static lifetime (which includes all variables defined at global scope) will have all of its data members initialized to 0 before anything else happens.

For example:

struct Foo {
    int x;
    std::string s;
};

Foo f1;

int main()
{
    Foo f2;
}

Here, f1.s and f2.s will both be initialized to null strings because that's what the default constructor for the library string class does. In addition, f1.x will be initialized to zero because f1 is a variable with static lifetime. On the other hand, f2.x will have an indeterminate value, and trying to use it without giving it a new value will cause undefined behavior.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.