1

When you declare a local variable i like so :

int i;

And you use this variable, you will get undefined behaviour because i isn't initialized yet. But i holds a value, a "garbage" value, where does this value come from? is it from a random place in memory?

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
  • 3
    The variable usually has a location in memory, in which case its value would be whatever happens to be in that memory location. – juanchopanza Jan 10 '15 at 17:19

2 Answers2

7

You don't know, you can't tell. Undefined behavior means that anything can happen.

That said, in practice on most implementations and most of the time int i; will reserve sizeof(int) bytes on the stack (which is usually somewhere in main memory), so the value of i will be whatever happens to be on the stack at this moment.

But don't rely on it, and remember that this isn't always true.

tux3
  • 7,171
  • 6
  • 39
  • 51
2

Its whatever was in memory at &i before.

Neil Locketz
  • 4,228
  • 1
  • 23
  • 34