Viktor Sehr
Viktor Sehr

Reputation: 13099

How to force value initialization for POD types in Visual C++ without changing code?

Is there any way to force initialization of pod types to 0\false\nullptr in Visual C++ release mode?

To be more specific, I don't want to change my code, just have it compiled with pod types initialized to 0\false\nullptr.

I want this because I want our system to be deterministic.

Upvotes: 4

Views: 761

Answers (1)

Collin Dauphinee
Collin Dauphinee

Reputation: 13993

Yes, value initialization will do this.

struct pod {
    int a, b;
    char c;
    double d;
};

pod myPod = pod(); // Value-initialized, all members are 0.

C++11 will also allow you to assign default values in the class definition, but Visual Studio doesn't support this yet.

struct pod { 
    int a = 0, b = 0;
    char c = 0;
    double d = 0.0;
};

pod myPod; // All members would be initialized to 0.

Upvotes: 5

Related Questions