1

I've tried on two different machines, and I get zeroes. Is it just a chance and it's garbage ?

#include <stdio.h>

int main()
{
    typedef union { int x; } union1;
    union1 u;
    printf("%d\n", u.x);
}

I'm aware of the compile warning that u isn't initialized, so, please don't include answers or comment about that. I want to know which of the following is the case:

  • It's compiler-dependent (If so, please include any official source for gcc)
  • It's always garbage and I was just lucky enough to find all zeroes on two different machines.
  • It's always zero (If so, please include any official source)
Youssef13
  • 3,836
  • 3
  • 24
  • 41
  • It's *Implementation Defined* from a compiler standpoint as well as dependent on the virtual memory manager for the OS, etc... So whether you were lucky or not -- just depends. Regardless, attempting to access a value with *automatic storage duration* while its value is *indeterminate* is *Undefined Behavior*. – David C. Rankin Jan 08 '20 at 15:35
  • @DavidC.Rankin it is not implementation defined. Standard defines it quite well. It is automatic storage object and it will not by zeroed. – 0___________ Jan 08 '20 at 15:37
  • @P__J__ I was talking about the state of the stack memory, not the values for the union member. (e.g. what you will find in that memory is like a box of chocolates -- you never know what you are going to get .. it just happened to be zero on those two occasions) – David C. Rankin Jan 08 '20 at 15:38
  • 1
    @DavidC.Rankin C standard does not know anything about the stack. – 0___________ Jan 08 '20 at 15:39
  • @DavidC.Rankin `you never know what you are going to get` it is called Undefined Behaviour not implementation defined :) – 0___________ Jan 08 '20 at 15:41
  • I see what you are saying.. The compiler doesn't have a dog in that fight. I agree. The implementation defined part doesn't assure the compiler does anything at all, but I didn't mean to imply it would. – David C. Rankin Jan 08 '20 at 15:42
  • Did you no read the rest of the statement "Regardless, attempting to access a value with *automatic storage duration* while its value is *indeterminate* is *Undefined Behavior.*"?? – David C. Rankin Jan 08 '20 at 15:43
  • Unions are not different from any other kind of variable here. All that matters is _storage duration_ of the variable. See the linked duplicate for examples when it is implementation-defined and when it is undefined behavior. – Lundin Jan 08 '20 at 15:48
  • @Lundin, It doesn't answer my question. I tried on many machines with gcc and got zero on all of them. I don't think I'm that lucky to get zero every time. So, I suspect GCC does something on that. But want some source. – Youssef13 Jan 08 '20 at 15:50
  • 1
    @Youssef13 You can't prove that it doesn't invoke undefiend behavior through trial and error. In philosophy they usually give the false premise: "all sheep I can see in every field I visit are white, so therefore I have proven that there are no black sheep". The link does answer the question, specifically [this](https://stackoverflow.com/a/40674888/584518). "In case the variable has automatic storage duration and does not have its address taken, the code always invokes undefined behavior". – Lundin Jan 08 '20 at 15:55
  • @Lundin, I've read it. But was suspecting that gcc zeroes it out by itself even if the standards doesn't say so. But after I found it garbage in release mode, I'm satisfied with proving that it's undefined behavior. – Youssef13 Jan 08 '20 at 15:58

3 Answers3

3

When a variable or aggregate with automatic storage duration is declared, it has an undefined value until it is explicitly assigned a value.

u has automatic storage duration, since it is declared in a function's scope and not explicitly declared static.

C Standard, § 6.7.9 p 10:

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.

Chances are on a modern system, you're just getting whatever is already in memory at the address, but this is by no means guaranteed.

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
0

static storage variables are zeroed if not initialized. Automatic variables are not.

0___________
  • 60,014
  • 4
  • 34
  • 74
0

C99 standard section 6.7.8 clause 10:

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.

If an object that has static storage duration is not initialized explicitly, then:

  • if it has pointer type, it is initialized to a null pointer
  • if it has arithmetic type, it is initialized to (positive or unsigned) zero
  • if it is an aggregate, every member is initialized (recursively) according to these rules
  • if it is a union, the first named member is initialized (recursively) according to these rules.

Essentially, for global variables (those defined outside functions), they are guaranteed to be zero. However, for variables defined inside functions it is undefined and could be anything, depending on the pre-existing memory layout of the program, your compiler, your system, and many other things.

Aplet123
  • 33,825
  • 1
  • 29
  • 55