Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to initialize C++ structs

Our code involves a POD (Plain Old Datastructure) struct (it is a basic c++ struct that has other structs and POD variables in it that needs to get initialized in the beginning.)

Based one what I've read, it seems that:

myStruct = (MyStruct*)calloc(1, sizeof(MyStruct)); 

should initialize all the values to zero, as does:

myStruct = new MyStruct(); 

However, when the struct is initialized in the second way, Valgrind later complains "conditional jump or move depends on uninitialised value(s)" when those variables are used. Is my understanding flawed here, or is Valgrind throwing false positives?

like image 240
KC3BZU Avatar asked May 06 '11 16:05

KC3BZU


People also ask

How do you initialize a struct in C?

Use Individual Assignment to Initialize a Struct in C Another method to initialize struct members is to declare a variable and then assign each member with its corresponding value separately.

How are structs initialized?

When initializing a struct, the first initializer in the list initializes the first declared member (unless a designator is specified) (since C99), and all subsequent initializers without designators (since C99)initialize the struct members declared after the one initialized by the previous expression.

Does C initialize structs to 0?

You don't have to initialise every element of a structure, but can initialise only the first one; you don't need nested {} even to initialise aggregate members of a structure. Anything in C can be initialised with = 0 ; this initialises numeric elements to zero and pointers null.

How do we initialise a structure give an example?

Initializing structure membersstruct car { char name[100]; float price; }; //car1 name as "xyz" //price as 987432.50 struct car car1 ={"xyz", 987432.50};


1 Answers

In C++ classes/structs are identical (in terms of initialization).

A non POD struct may as well have a constructor so it can initialize members.
If your struct is a POD then you can use an initializer.

struct C {     int x;      int y; };  C  c = {0}; // Zero initialize POD 

Alternatively you can use the default constructor.

C  c = C();      // Zero initialize using default constructor C  c{};          // Latest versions accept this syntax. C* c = new C();  // Zero initialize a dynamically allocated object.  // Note the difference between the above and the initialize version of the constructor. // Note: All above comments apply to POD structures. C  c;            // members are random C* c = new C;    // members are random (more officially undefined). 

I believe valgrind is complaining because that is how C++ used to work. (I am not exactly sure when C++ was upgraded with the zero initialization default construction). Your best bet is to add a constructor that initializes the object (structs are allowed constructors).

As a side note:
A lot of beginners try to value init:

C c(); // Unfortunately this is not a variable declaration. C c{}; // This syntax was added to overcome this confusion.  // The correct way to do this is: C c = C(); 

A quick search for the "Most Vexing Parse" will provide a better explanation than I can.

like image 56
Martin York Avatar answered Oct 02 '22 18:10

Martin York