You are not logged in.

#1 2015-12-24 12:05:11

bstaletic
Member
Registered: 2014-02-02
Posts: 658

[SOLVED] C/C++ Uninitialized local variables have 0 value consistently

I know the standard is to only initialize global and static variables, but not auto variables. How come I always have 0 as the value for uninitialized variables, no matter what compiler I use (tried with gcc, g++, clang, clang++, tcc).

Following code will output 1 on my laptop:

#include <stdio.h>

int main(int argc, char *argv[]){
    int i;
    i++;
    printf("%d",i);
    return 0;
}

Note: Floats also get initialized to 0, and it doesn't matter if it's c or c++ code.

Last edited by bstaletic (2015-12-24 13:10:28)

Offline

#2 2015-12-24 12:24:36

ayekat
Member
Registered: 2011-01-17
Posts: 1,589

Re: [SOLVED] C/C++ Uninitialized local variables have 0 value consistently

Variables are not "initialised", but they rather just take the value that was at their memory before.

Now while it may seem odd that some random memory location always holds the value 0 before your program "occupies" it, keep in mind that in order for your program to be executed, the operating system needs to do some setup work (like allocating pages for the process, setting up stack pointers, setting up the stack with arguments, the heap, etc.), and although I cannot tell you the exact memory layout of where data goes before your program launches, we know that it's on the stack, and I'm pretty sure that stack used by that "setup" procedure overlaps with the stack used by your program.

To give you a less limited view on your program's memory/variables, try this:

#include <stdio.h>

int main(void)
{
	int a, b, c, d, e, f, g, h, i, j, k, l;

	printf("a = %d\n", a);
	printf("b = %d\n", b);
	printf("c = %d\n", c);
	printf("d = %d\n", d);
	printf("e = %d\n", e);
	printf("f = %d\n", f);
	printf("g = %d\n", g);
	printf("h = %d\n", h);
	printf("i = %d\n", i);
	printf("j = %d\n", j);
	printf("k = %d\n", k);
	printf("l = %d\n", l);
	return 0;
}

EDIT Try executing it multiple times - you'll notice that some variables seem to be "stuck" at 0, some seem to increase at each execution, and some might be interesting to see their value in hexadecimal, and to compare with a few pointers (like the stack pointer, or the address of the arguments, ...)

Last edited by ayekat (2015-12-24 12:34:08)


pkgshackscfgblag

Offline

#3 2015-12-24 12:50:59

bstaletic
Member
Registered: 2014-02-02
Posts: 658

Re: [SOLVED] C/C++ Uninitialized local variables have 0 value consistently

Interesting. Just tried your example and variables a,b,e,g,i,j,k are always zero, f,h and l are constant and the only variables changing are c and d. d seems truely random, while c is either 0x7ffd, 0x7ffe or 0x7fff.

This is nice to know. I thought I may need to fight the overhead of initializing, as I'm actually dealing with microcontrollers.

How do I compare to stack pointer?

Thanks, marking as solved.

EDIT:

Comparison to stack pointer is interesting. Concatenated hex values of c and d variables (disregarding the leading 0x) gave me the value of stack pointer.

Last edited by bstaletic (2015-12-24 13:09:41)

Offline

#4 2015-12-24 20:03:42

mpan
Member
Registered: 2012-08-01
Posts: 1,188
Website

Re: [SOLVED] C/C++ Uninitialized local variables have 0 value consistently

Both C and C++ define the value as undefined. "Undefined" means it may be anything, including being initialized to 0, taking previous value of the memory, being initialized to 0xDEADBEEF or consecutive bytes of string "blarg! blarg! blarg!", or anything else. In modern operating systems memory is usually zeroed at start and hence short-lived programs will typically have 0s everywhere, hence the value is equal to what 0s mean. Note that few bytes equal to 0 under a variable of type other than char are not required to cause the variable to be equal to 0! It's just a common convention, but nothing guarantees the behaviour.

Basically you're getting a random value, which happens to always be 0. But it's not guaranteed to be 0.


Sometimes I seem a bit harsh — don’t get offended too easily!

Offline

Board footer

Powered by FluxBB