0

I am new to c++ and I want to learn best practice of c++. I got one question, does the modern c++ compiler will auto assign default value for an uninitialized variable? If yes, does it mean that we do not need to assign default value to a variable or it depends on system?

Thank you for your help and clarification.

overshadow
  • 958
  • 2
  • 15
  • 31
  • 1
    possible duplicate of [Variable initialization in C++](http://stackoverflow.com/questions/2218254/variable-initialization-in-c) – Cory Kramer Oct 14 '14 at 17:07

3 Answers3

4

Only static and global data is always initialised...

int w; // will be 0
static int x; // will be 0

void f() { static int x; /* will be 0 the first time this scope's entered */ }

struct S
{
    int n_;
};

S s_;  // s_.n_ will be 0 as s_ is global

int main()
{
    S s;  // s.n_ will be uninitialised
          // undefined behaviour to read before setting
}

For any other variables they must have - at some level in the code - explicit initialisation before they're read from. That might not be visible at the point a variable is declared though - it could be in a default constructor or an assignment. You can also cause initialisation like this:

int x{};  // will be 0
int* p = new int{};  // *p will be 0 
Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • What kind of data type or variable do not initialised automatically? Can you provide some example? – overshadow Oct 14 '14 at 17:16
  • Is it this one? --> int main() { int y; //this one have unknown value} – overshadow Oct 14 '14 at 17:22
  • @overshadow: examples: `long`, `int`, `short`, `char`, `double`, `float`, `bool` - it's mainly classes and structs - like `std::string`, `std::vector`, `std::smart_ptr` that will always default construct themselves with a sane, usable state. – Tony Delroy Oct 14 '14 at 17:31
  • but why your first example (int w; // will be 0) will auto initialise? Is it because it is a global variable? – overshadow Oct 14 '14 at 17:33
  • @overshadow: yes, `int` won't *always* be initialised for you, but it will be if it's global or static. – Tony Delroy Oct 14 '14 at 17:35
1

Default initialization is performed in three situations:
1) when a variable with automatic, static, or thread-local storage duration is declared with no initializer.
2) when an object with dynamic storage duration is created by a new-expression with no initializer or when an object is created by a new-expression with the initializer consisting of an empty pair of parentheses (until C++03).
3) when a base class or a non-static data member is not mentioned in a constructor initializer list and that constructor is called.

More information here: http://en.cppreference.com/w/cpp/language/default_initialization

Laura Maftei
  • 1,863
  • 1
  • 15
  • 25
0

With regard to what the compiler will do I think its more of the inverse, for example:

int x; // still must be inited, will contain random data
if (some_func())
{
   // some devs will do this for "performance" - i.e don't assign a value to x
   x = 2;
}

But if you write:

int x = 0;
if (some_func())
{
   x = 2;
}

The compiler will optimize this to:

int x;
if (some_func())
{
   x = 2; // Yes this code is actually the first example again :)
}

Assuming x is not used else where in the function.

paulm
  • 5,629
  • 7
  • 47
  • 70