3

In 'The C programming language' third edition and on p.32 I saw those lines which make me confused:

Because automatic variables come and go with function invocation, they do not retain their values from one call to the next, and must be explicitly set upon each entry. If they are not set, they will contain garbage

Is it saying that for the following code, a will not contain garbage after the program finished its execution and if I declare a like this: int a; then a will contain garbage?

#include <stdio.h>
int main () {
        int a = 5;
        // int a;
        printf("\n\t %d", a);
}
Khanh Tran
  • 1,776
  • 5
  • 25
  • 48
  • 4
    It doesn't contain garbage. It is undefined what is contained. On windows it would probably be 0, but it could be flying monkey dishwashers according to the C language. – Elliott Frisch Aug 21 '14 at 15:31
  • 3
    The problem is that it *often* will contain `0`, but not always. Programmers who fail to initialize their automatic variables will see that it works most of the time...which is much worse than having it fail consistently. – nneonneo Aug 21 '14 at 15:33
  • basically i contains garbage left by last executed program. – Jason Hu Aug 21 '14 at 15:33
  • @ElliottFrisch This explains the monkey in my room. – Maroun Aug 21 '14 at 15:33
  • Are you guys drunk? Prints 5 here. Compiler: my head. – Igor Pejic Aug 21 '14 at 15:34
  • @MarounMaroun At least the dishes are getting "done". – Elliott Frisch Aug 21 '14 at 15:34
  • 5
    @Igor I'm high not drunk, but I still know that OP is referring to the commented part in his code. – Maroun Aug 21 '14 at 15:34
  • @Igor: I was talking about uninitialized automatic variables, not the OPs specific code. – nneonneo Aug 21 '14 at 15:36
  • 1
    @HuStmpHrrr It will not contain data "left by last executed program" on any modern (multi-user, protected memory) operating system. That would be a huge security hole. – nobody Aug 21 '14 at 15:38
  • To be ridiculously pedantic, the correct term is "indeterminate". It will *most likely* be something other than all-bits-zero, and it may not even be a valid value for that type. – John Bode Aug 21 '14 at 17:49

4 Answers4

10

Using the value of a not initialised variable is undefined behaviour. In practice automatic variables are allocated in processor registers or on the stack. Normally if not initialised they will get the value present in the register or memory used for the stack at the moment. So e.g. an int variable may contain a part of memory which was double variable in a function called just a moment ago. In other words the value is random.

Wojtek Surowka
  • 20,535
  • 4
  • 44
  • 51
  • 4
    Or, if you don't have a braindead compiler, it'll be a register. The contents of which will also be undefined... – EOF Aug 21 '14 at 15:47
2

Consider the following example:

#include <stdio.h>

void fun1(void)
{
    int num1;
    printf("fun1: %i\n", num1);
}

void fun2(void)
{
    int num2 = 7;
    printf("fun2: %i\n", num2);
}

int main(void)
{ 
    fun1();
    fun2();
    fun1();

    return 0;
}

On my machine, I get the following output:

fun1: 0
fun2: 7
fun1: 7

As you can see, because num1 is not initialized, it has the value of whatever is on the stack at the time of the call.

You can visualize the stack like this during program execution like this:

    main     fun1     main     fun2     main     fun1     main
    [0]  SP->[0]      [0]  SP->[7]      [7]  SP->[7]      [7]
SP->[x]      [x]  SP->[x]      [x]  SP->[x]      [x]  SP->[x]

Legend: SP = Stack Pointer, x = Don't Care
Fiddling Bits
  • 8,712
  • 3
  • 28
  • 46
1

If you don't initialize the variable before using it it will just get filled with whatever the computer had in the memory space where the variable is located, that's why it is good practice to always give them an initial value, even if it is zero.

Sathania
  • 69
  • 1
  • 1
  • 7
0
  1. Auto variables are allocated on stack. Stack change it's size very often. Pushing on stack does not overwrite memory, but just move stack pointer. So, function frame (saved registers, local storage and local variables of function) is placed on stack instead of other frame and overlap some information.
  2. Static and global variables are placed in data segment or bss segment. This segments are filled with zeros due to security reasons. Imagine, that you didn't fill this segment with zeros. As computer doesn't overwrite memory new program can get access to information of previous program that was allocated on this place.
  3. There are also register variables, we ask compiler to place them in registers. But it is not guaranteed, because compiler may decide to substitute them with auto variables.
Ivan Ivanov
  • 2,076
  • 16
  • 33
  • C does not specify that "Auto variables are allocated on stack". Neither does C specify "Static and global variables are placed in data segment or bss segment.". Many memory models exist. – chux - Reinstate Monica Aug 21 '14 at 19:04