1

I have a question, just doing some test, trying to build a iterator (Opp) in c, but when i started, I saw that when i create a struct that only had "int a" and "int b", the struct would initialize the values by itself at 0 (I know that this is undefined behavior and i shouldn't do it) but everytime I run the code the values where 0, but when i added a string to a struct, the string is set to null by itself, but the integers add garbage values (even if i try to pack the struct, I thought it was because of the padding of the struct), can someone explain me if this is just random or does it have a reason?

typedef struct st {
    int a;
    int b;
} st;

int main(void)
{
    st my;

    printf("a: %d\n", my.a); // always 0
    printf("b: %d\n", my.b); // always 0
    return (0);
}
typedef struct st {
    int a;
    int b;
    char *str;
} st;

int main(void)
{
    st my;

    printf("a: %d\n", my.a); // Random value
    printf("b: %d\n", my.b); // Random value
    printf("str: %s\n", my.str); // Always NULL
    return (0);
}
  • 2
    0 may just as well be a "garbage value" as any other number – UnholySheep Dec 02 '21 at 14:48
  • But always? I would understand if it happen 3 out of 10 times, but happens every time – ricardomart Dec 02 '21 at 14:49
  • 2
    Your code has a lot of commented out code, and it is not clear what outputs what. I suggest you get a snippet without commented out code, and give indication of the output in place. – SergeyA Dec 02 '21 at 14:49
  • Why not? the calling code for `main()` is probably the same each time (apart from address space randomization). There are far more zeros in the world than "funny (garbage)" numbers. – Weather Vane Dec 02 '21 at 14:51
  • Yes, "always" - In debug mode some compilers zero out things. It's not mandated behavior, nor illegal (since again, 0 is as much "garbage" as any value when garbage is permitted). – StoryTeller - Unslander Monica Dec 02 '21 at 14:51
  • That s the nature of Undefined Behaviour. It is simple undefined. Question, why undefined is defined, makes no sense Read about it. – 0___________ Dec 02 '21 at 14:54
  • Similar question: You close your eyes and pass the busy crossing unharmed, does it guarantee that every time you do the same no one will drive you over? – 0___________ Dec 02 '21 at 14:59
  • Some bad tool chains set the stack to zero when in debug mode, hiding bugs like this under the carpet until you switch to release mode and everything crashes. This is a feature designed to waste as much of your time as possible, ensuring that you don't find bugs at the point you write them, but much later on. – Lundin Dec 02 '21 at 15:04
  • @0___________: Read about it where? There is no reputable source that says undefined behavior “makes no sense”. The C standard’s definition of “undefined behavior” is that the standard imposes no requirements. Nowhere does it say that undefined behavior makes no sense or is not caused or influenced by things other than the C standard. – Eric Postpischil Dec 02 '21 at 15:04
  • @ricardomart: Yes, there are reasons for the behavior you observe. They lie in how programs are constructed and executed. They are not controlled by the C standard, so you cannot rely on them remaining the same without understanding those reasons and their causes. Before your `main` routine executes, start-up code runs to set up the C environment, and it leaves a few things in memory as it does so. Those account for some initial values you observe. Optimization or lack thereof by the C compiler also affects this. Some randomness is deliberately injected to foil attackers. – Eric Postpischil Dec 02 '21 at 15:07
  • Uninitialized memory and uninitialized variables have indeterminate value, it might be zero, or some random value, or give a random value each time you read it, or it could trap when used. You should never assume anything about the underlying value. – Hasturkun Dec 02 '21 at 16:03

0 Answers0