-2

Problem: Local variable is getting initialized to zero, but if assign the variable to a pointer it is not getting initialized to zero.

Code 1: Local variable is getting initialized to zero (I have tested it may times, a is always getting set to zero)

int main() {
    int a;
    printf ("a = %d\n",a);
    return 0;
}

OUTPUT:
a = 0 ( I have called this about a 100 times, and 'a' is always zero)

Code 2: Local variable is NOT getting initialized to zero (I have tested it may times, a is never getting set to zero)

int main() {
    int a;
    printf ("a = %d\n",a);
    nt *b = &a;
    printf("*b = %d\n",*b);
    return 0;
}

OUTPUT:
a = 21683 (everytime I run, I get different values as expected)
*b = 21683

I expected a to have random values as it is not a static variable. Can anyone has any idea as to why this happening? Is this something in the standard or some feature of the compiler.

PS: I am using gcc.

I have tried different optimization options, even -o0 but the result is the same.

Arjob Mukherjee
  • 387
  • 1
  • 10
  • 1
    Undefined behavior is undefined. What you're seeing is consistent with that. And `main` returns `int`, not void. – Mat Jan 06 '19 at 09:44
  • It is undefined behavior – incompetent Jan 06 '19 at 09:44
  • 1
    Uninitialized local variables (a.k.a. *automatic* variables) are *uninitialized* and will have an *indeterminate* value. That's part of the C specification, and that you seem to be getting zero all the time is just pure coincidence (or due to some compiler implementation-specific behavior). – Some programmer dude Jan 06 '19 at 09:47
  • @Mat I actually have used int main(). This is a typo. – Arjob Mukherjee Jan 06 '19 at 09:49
  • It is undefined behavior. but an idea for that constant zero vs not is a potential use of registers or other optimizations- Not to be trusted to repeat. in your first code there is a possibility(but you can not really know that for sure) that a is kept on one, or some other type of optimization is being done for that outcome, but in your second code, int a must be kept on stack so that b can point to a legitimate address, and therefor a gets that junk value as expected. – H.cohen Jan 06 '19 at 10:03
  • There is no "automatic initialization of local variables in C", with local meaning "automatic", i.e. on the stack. They are left _un_ initialized. – Paul Ogilvie Jan 06 '19 at 11:08
  • The Compiler makes jokes with you. – Michi Jan 06 '19 at 11:18

2 Answers2

3

The behavior is entirely unpredictable. Zero is just as much garbage in this context as anything else. By the way, I happen to get different results:

$ cat a.c
#include <stdio.h>

void f1() {
    int a;
    printf ("f1: a = %d\n",a);
}

void f2() {
    int a;
    printf ("f2: a = %d\n",a);
    int *b = &a;
    printf("f2: *b = %d\n",*b);
}

int main()
{
    f1();
    f2();
}

$ gcc a.c -O2 -o a
$ ./a
f1: a = 0
f2: a = 0
f2: *b = 0

$ gcc a.c -o a
$ ./a
f1: a = 22011
f2: a = 22011
f2: *b = 22011
David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • I In the pointer code, settings optimisation higher than -O0, 'a' and '*b' is geting set to 0, when. Using -O0 it is random values. I wonder what is causing the consistency. – Arjob Mukherjee Jan 06 '19 at 10:37
  • @ArjobMukherjee If you're really super curious, you can test optimization options one by one to figure out the one that's doing it and compare the generated assembly code. But you might learn something that's not true on other compiler versions, other compilers, other platforms, other CPUs, etcetera. – David Schwartz Jan 06 '19 at 22:06
0

There's no automatic initialization of automatic (local in your parlance) variables in C.

Your options:

  1. don't use C (maybe not even C++ as it has a similar issue)
  2. initialize each of them as needed whether you like it or not
  3. if the structure and logic of your code permit, make them static (or even move them outside of the function body) and they will become initialized to 0 (or NULL) at program start
  4. define a structure containing all of those variables, then do memset(&structure, 0, sizeof structure); on it

There really isn't a way around undefined behavior as it has already been mentioned by others. You must avoid it (for that you'll need to learn what triggers it, there's much more to it than uninitialized variables).

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180