Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typedef struct : Default Initialization

typedef struct foo
{
    bool my_bool;
    int my_int;
} foo;

In the example above I understand that my_bool will be initialized randomly to either true or false but what about my_int? I assumed that my_int would be default initialized to 0 but that seems not to be the case.

Defining structs in this way appears to be incompatible with initialization lists so what is the best way to initialize my_bool and my_int to false and 0 respectively?

like image 553
Stephen Blinkhorn Avatar asked Jun 30 '10 21:06

Stephen Blinkhorn


People also ask

Are structs default initialized?

Constructors are defined with a function name of this and have no return value. The grammar is the same as for the class Constructor. A struct constructor is called by the name of the struct followed by Parameters. If the ParameterList is empty, the struct instance is default initialized.

Can you set default values in a struct?

Default values can be assigned to a struct by using a constructor function. Rather than creating a structure directly, we can use a constructor to assign custom default values to all or some of its members. Another way of assigning default values to structs is by using tags.

Are struct members initialized to zero?

If a structure variable does not have an initializer, the initial values of the structure members depend on the storage class associated with the structure variable: If a structure variable has static storage, its members are implicitly initialized to zero of the appropriate type.

Can we set default value in struct in C?

Default values MAY be defined on struct members. Defaults appear at the end of a field definition with a C-like = {value} pattern.


2 Answers

Types don't get "initialized". Only objects of some type get initialized. How and when they get initialized depends on how and where the corresponding object is defined. You provided no definition of any object in your question, so your question by itself doesn't really make much sense - it lacks necessary context.

For example, if you define a static object of type foo

static foo foo_object; // zeros

it will be automatically zero-initialized because all objects with static duration are always automatically zero-initialized.

If you define an automatic object of type foo without an initializer, it will remain uninitialized

void func()
{
   foo foo_object; // garbage
}

If you define an automatic object of type foo with an aggregate initializer, it will be initialized in accordance with that initializer

void func()
{
   foo foo_object1 = { 1, 2 }; // initialized
   foo foo_object2 = {}; // initialized with zeros
}

If you allocate your object with new and provide no initializer, it will remain uninitialized

foo *p = new foo; // garbage in `*p`

But if you use the () initializer, it will be zero-initialzed

foo *p = new foo(); // zeros in `*p`

If you create a temporary object of type foo using the foo() expression, the result of that expression will be zero-initialized

bool b = foo().my_bool; // zero
int i = foo().my_int; // zero

So, once again, in your specific case the initialization details depend on now you create the object of your type, not on your type itself. Your type itself has no inherent initialization facilities and doesn't interfere with the initialization in any way.

like image 52
AnT Avatar answered Oct 06 '22 04:10

AnT


Implement a default constructor:

typedef struct foo 
{ 
    foo()
    : my_bool(false), my_int(0)
    {
        // Do nothing
    }
    bool my_bool; 
    int my_int; 
} foo; 
like image 41
rturrado Avatar answered Oct 06 '22 03:10

rturrado