Click here to Skip to main content
12,884,480 members (28,118 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 25 Feb 2014

Variable Declaration and Initialization

, 25 Feb 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Variable Declaration and Initialization


Initialization in C++ is a complex topic!
Some of the initialization types of C++ are the following:

  • Default Initialization: If a variable is not initialized explicitly, the value is garbage.
  • Value Initialization: Initializes with the type’s default initial value (e.g. default constructor for user types, zero for number types).
  • Direct Initialization: Initializes with the given value.

The example below shows what happens to variables when initialized with these different initialization types.

#include <iostream>
#include <cstdint> // uint64_t
#include <algorithm> // fill
using namespace std;

static uint64_t garbage[32];
template<typename T>
void print(const string& m, const T& a)
{ cout << m << ": (" << a.d << "," 
<< a.f << "," << a.s << "," << a.i << ") "
       << endl; }
static void reset() { fill(begin(garbage), end(garbage), 0xDEADBEAF); }

class A {
public: double d;  float f;  string s; int i;

class B {
public: double d;  float f;  string s; int32_t i;
  B() {}
  explicit B(int8_t a) : i{a} {}
  explicit B(uint8_t a) : d{}, f{}, s{}, i{a} {}
  explicit B(int16_t a) : d{}, s{}, i{a} {}
  explicit B(double ad, float af, string as, int ai)
           : d{ad}, f{af}, s{as}, i{ai} {}

int main() {

  auto v1 = new (garbage) A; // Placement New syntax.

  print("Default", *v1); reset();

  auto v2 = new (garbage) A{};
  print("Value without default ctor", *v2); reset();

  auto v4 = new (garbage) B{};
  print("Value with default ctor", *v4); reset();

  auto v5 = new (garbage) B{int8_t{2}};
  print("Direct with Default in ctor", *v5); reset();

  auto v6 = new (garbage) B{uint8_t{2}};
  print("Direct with Value in ctor", *v6); reset();

  auto v7 = new (garbage) B{int16_t{2}};
  print("Direct with Value and Default in ctor", *v7); reset();

  auto v8 = new (garbage) B{1.0, 2.0, "Moikkelis!", 3};
  print("Direct with Direct in ctor", *v8); reset();

  auto v3 = new (garbage) A{1.0, 2.0, "Hello!", 3};
  print("Aggregate", *v3); reset();

  return 0;

Here is the output of the above code:

Default: (1.84579e-314,-6.25982e+18,,-559038801) 
Value without default ctor: (0,0,,0) 
Value with default ctor: (1.84579e-314,-6.25982e+18,,-559038801) 
Direct with Default in ctor: (1.84579e-314,-6.25982e+18,,2) 
Direct with Value in ctor: (0,0,,2) 
Direct with Value and Default in ctor: (0,-6.25982e+18,,2) 
Direct with Direct in ctor: (1,2,Moikkelis!,3) 
Aggregate: (1,2,Hello!,3) 

The “ugly” floating point numbers represent uninitialized variables. As you can see, sometimes initialization does not happen even though you thought it would.

Here are some tips in order to avoid common pitfalls:

  • Always initialize during declaration!
  • Prefer Direct Initialization.
  • Careful with Value Initialization of user types with user-defined default constructor: It can leave things uninitialized if the programmer accidentally left some member variables Default Initialized.
  • Avoid Default Initialization (i.e. no initialization).




This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Gabor Fekete
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170422.1 | Last Updated 25 Feb 2014
Article Copyright 2014 by Gabor Fekete
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid