Does a c++ struct have a default constructor?

C++

C++ Problem Overview


I wrote the following code snippet:

void foo()
{
    struct _bar_ 
    {
        int a;
    } bar; 

    cout << "Value of a is " << bar.a;
}

and compiled it with g++ 4.2.1 (Mac). The output is "Value of a is 0".

Is it true to say that data members of a struct in c++ are always initialized by default (compared to c)? Or is the observed result just coincidence?

I can imagine that structs in c++ have a default constructor (since a struct and a class is almost the same in c++), which would explain why the data member a of bar is initialized to zero.

C++ Solutions


Solution 1 - C++

The simple answer is yes.
It has a default constructor.

Note: struct and class are identical (apart from the default state of the accesses specifiers).

But whether it initializes the members will depends on how the actual object is declared. In your example no the member is not initialized and a has indeterminate value.

void func()
{
    _bar_  a;                 // Members are NOT initialized.
    _bar_  b = _bar_();       // Members are zero-initialized
    // From C++14
    _bar_  c{};               // New Brace initializer (Members are zero-initialized)


    _bar_* aP = new _bar_;    // Members are NOT initialized.
    _bar_* bP = new _bar_();  // Members are zero-initialized
    // From C++14
    _bar_  cP = new _bar_{};  // New Brace initializer (Members are zero-initialized)
}

// static storage duration objects
//   i.e. objects at the global scope.
_bar_ c; // Members are zero-initialized.

The exact details are explained in the standard at 8.5 Initializers [dcl.init] paragraphs 4-10. But the following is a simplistic summary for this situation.

A structure without a user defined constructor has a compiler generated constructor. But what it does depends on how it is used and it will either default initialize its members (which for POD types is usually nothing) or it may zero initialize its members (which for POD usually means set its members to zero).

PS. Don't use a _ as the first character in a type name. You will bump into problems.

Solution 2 - C++

>Is it true to say that data members of a struct in c++ are always initialized by default (compared to c)? Or is the observed result just coincidence?

It is a coincidence.

Your code invokes Undefined Behavior; unless you explicitly set the members to 0 they can be anything.

Solution 3 - C++

Not an answer, but you might take it to be... if you want to try it:

void foo() {
   struct test {
      int value;
   } x;
   std::cout << x.value << std::endl;
   x.value = 1000;
}
int main() {
   foo();
   foo();
}

In your example, the memory already had the 0 value before the variable was created, so you can call it a lucky coincidence (in fact, some OS will zero out all memory before starting a process, which means that 0 is quite a likely value to find in a small short program...), the previous code will call the function twice, and the memory from the first call will be reused in the second one, chances are that the second time around it will print 1000. Note however that the value is still undefined, and that this test might or not show the expected result (i.e. there are many things that the compiler can do and would generate a different result...)

Solution 4 - C++

Member variables of a struct are not initialized by default. Just like a class (because a struct is exactly the same thing as a class, only in struct the members are public by default).

Solution 5 - C++

Do not rely on this functionality it is non-standard

just add

foo() : a() {}

I can't remember the exact state of gcc 4.2 (i think it is too old) but if you were using C++11 you can do the following

foo()=default;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmorpheusView Question on Stackoverflow
Solution 1 - C++Martin YorkView Answer on Stackoverflow
Solution 2 - C++Alok SaveView Answer on Stackoverflow
Solution 3 - C++David Rodríguez - dribeasView Answer on Stackoverflow
Solution 4 - C++IgorView Answer on Stackoverflow
Solution 5 - C++111111View Answer on Stackoverflow