1

When I try to explain some simple code to my friends,something strange happens:

#include <stdio.h>
int main() 
{
    int x;
    printf("%x\n",x);
}

I have tryed millions of times ,and the last 12 bits of x always turns out to be 0xff0. I've disass the code,but still can't figure out what's going on here

My operating system is ubuntu10.10 ,compiler is gcc4.7.2

FooBee
  • 778
  • 7
  • 23
  • 2
    Is it just even, or is it a multiple of 4 or 8? – ChrisW Dec 07 '12 at 17:22
  • 1
    Why is this question being closed? It's a valid programming question with a simple answer? – Preet Sangha Dec 07 '12 at 17:31
  • @ChrisW I found it's not just even, the last 3 bits always turns out to be 0xff0.I've edited my question. thank you! – FooBee Dec 07 '12 at 17:39
  • @PreetSangha It's unclear, because 0xff0 is 12 bits, not 3. It's the very definition of “too localized”. It duplicates a number of easy-to-find SO questions such as http://stackoverflow.com/questions/11233602/what-will-be-the-value-of-uninitialized-variable – Pascal Cuoq Dec 07 '12 at 17:40
  • 1
    0xFF0 is only a byte and a half. – Hot Licks Dec 07 '12 at 17:43

4 Answers4

6

Reading an uninitialized variable is undefined behavior. So you are guaranteed nothing.

This includes always being zero, always being even, always being different, and reformatting your hard drive.

GManNickG
  • 494,350
  • 52
  • 494
  • 543
4

First of all, if the value always matches 0xFF0 then the "lower 12 bits" are that value, not the lower 3.

As for why this happens, you're reading a variable that you haven't initialized. That's undefined behavior, and anything could have happened when you tried to do this, from a crash to a pizza getting delivered.

But what actually happens is this: the location that x references has some data that it held previously. Chances are that on your setup, that variable happens to contain data whose lower twelve bits are 0xFF0; in which case it's most likely data associated a previous system call, during processing work done by the system before your main is called.

To make a long story short: initialize your variables before using them and don't ask why the uninitialized ones have the value they do.

Nik Bougalis
  • 10,495
  • 1
  • 21
  • 37
2

The value is always the same because your program is deterministic.

The value of an uninitialized variable is officially "undefined", but in a program that always goes through the exact same steps it may well be predictable. In this case it turns out that whatever used that register or stack slot before main needed the value you see.

The only time a program would not be deterministic is if it interacts with something else that is non-deterministic (such as a user, or a changeable file, or the network). A threaded program may be non-deterministic without any external stimuli, however.

ams
  • 24,923
  • 4
  • 54
  • 75
0

I found it's not just even, the last 3 bits always turns out to be

Maybe the previous thing that was stored in that location on the stack is the value of a pointer to heap-allocated memory, or something like that (heap-allocated blocks are often rounded to a boundary, e.g. an 8-byte boundary).

ChrisW
  • 54,973
  • 13
  • 116
  • 224