2

The question that was put forth to me was:

What will the value of uninitialized variables be in C++? Do we have to initialize all variables? What would be the rule for initializing variables?

I've looked in my text as well as another text that I have on hand and can't seem to find the answer. Here's what I've attempted:

The value of uninitialized variables in C++ depends on the previous value stored in the memory that the uninitialized variable is assigned to. Initializing all variables is not a requirement, e.g. a variable does not need to be initialized if the variable will immediately be assigned a value via user input. Good programming practice should be to initialize a variable and if a variable is uninitialized, then comments should explain the reasoning behind leaving the variable uninitialized.

Am I missing something? Is their a resource anyone could point me to? Is there a "rule" to variable initialization that I missed? Thank you.

  • 3
    the value is undefined -- the only right answer – Ryan Oct 26 '15 at 00:25
  • Is it really undefined though? I mean, if you declare a variable and don't initialize it, then print the variable to the console, you'll still get a value. Where do you get that it's "undefined"? Thank you. –  Oct 26 '15 at 00:29
  • I get my answer from the standard – Ryan Oct 26 '15 at 00:30
  • If you want to learn more, I'd recommend reading the presentation "deep C (and C++)" by Olve Maudal. There is also a YouTube video: http://youtube.com/watch?v=z-RJK-NBKXE – Christoph Sommer Oct 26 '15 at 00:30
  • It looks like you are having troubling understanding what "undefined" means in practice, let me give you an example. Suppose you have an uninitialized bool. Both `b == true` and `b == false` could evaluate to true if `b` is uninitialized. – Neil Kirk Oct 26 '15 at 00:35
  • Basically undefined means that you have no idea what the value of the variable is because you didn't assign a value to it? –  Oct 26 '15 at 00:39
  • It's not just that you don't know what the value is - it's that it does not have a specific value at all. – Neil Kirk Oct 26 '15 at 01:00
  • Please see [Is uninitialized local variable the fastest random number generator?](http://stackoverflow.com/questions/31739792/is-uninitialized-local-variable-the-fastest-random-number-generator/31746063#31746063) – Shafik Yaghmour Oct 26 '15 at 01:07

4 Answers4

2

Generally, you have no idea what is stored in an uninitialized variable so it is always a good idea to initialize variables. This way you can avoid possible confusion later on; for example, if you went to print out a variable later on in your program for debugging (and you hadn't initialized it yet) it would likely print out some strange value.

If you want some more info, it looks like your question was also answered here: http://www.cplusplus.com/forum/general/62807/

Logan
  • 1,575
  • 1
  • 17
  • 26
  • 2
    ***it would likely print out some strange value.*** Or even worse it could print a random value that is in a range you expect making you believe your program is operating correctly. – drescherjm Oct 26 '15 at 03:36
0

Pretty good. Don't forget that variables with global scope (globals and static locals) are initialized to zero at program initialization.

Dúthomhas
  • 8,200
  • 2
  • 17
  • 39
0

For global and static variables including static class members, they will be initialized to zero/NULL. Everything else is usually undefined, meaning can be anything. An exception are in high security environments, the system may clear released memory, in which case if you use it, it will be all zeroes. Another exception can be debug builds, where special magic numbers are sometimes used to initialize memory and variables as a way to detect some common programming errors.

Yimin Rong
  • 1,890
  • 4
  • 31
  • 48
0

The variable is declared but is not set to a definite known value before it is used. It will have some value, but not a predictable one. I suggest you to take a look at this page http://www.learncpp.com/cpp-programming/eight-c-programming-mistakes-the-compiler-wont-catch/

cbInfo009
  • 71
  • 1
  • 4
  • 14