Initialize a structure pointer to NULL, then try to change its members

74 Views Asked by At

If you initialize a structure pointer to NULL, and try to change its members, is that what you call undefined behaviour(UB)?

I have this code:

#include <stdio.h>
typedef struct aStructure {
    int testInt;
}aStructure;


int main(void) {

    aStructure * a=NULL;
    a->testInt = 123;
    printf("%d", a->testInt);

}

This happens when I run it:

  • I get no warnings or error messages.
  • It takes some seconds for it to stop.
  • It prints nothing.

I am wondering a little what goes on "under the hood" here? When I initialize a structure pointer without having initialized the structure, does C then "set something aside" with the correct members? Because it seems that I can initialize the members without having the structure intialized, only the pointer?

2

There are 2 best solutions below

0
Eric Postpischil On BEST ANSWER

If you initialize a structure pointer to NULL, and try to change its members, is that what you call undefined behaviour(UB)?

Only a lot.

This happens when I run it:…

C is not one thing. The C standard specifies a base language. Compilers vary in how they implement and extend that language. When asking about what specific thing happens in your program with undefined behavior, you need to completely specify the C implementation you used, including the compiler, its version, the switches you compiled with, and the hardware and operating system you ran the program on.

It takes some seconds for it to stop.

This is unusual. In most C implementations, the program would either immediately crash or immediately print something and complete execution.

When I initialize a structure pointer without having initialized the structure, does C then "set something aside" with the correct members?

No.

Because it seems that I can initialize the members without having the structure intialized, only the pointer?

It is not correct to draw this conclusion from the behavior you report. Taking some seconds for the program to stop and printing nothing is not an indication that the program successfully initialized any member of a structure.

2
Lundin On

I am wondering a little what goes on "under the hood" here?

Lets have a look! gcc 13.x -O3 Linux x86_64 translates your program into the following machine code:

main:
    mov     DWORD PTR ds:0, 0
    ud2

It sets a memory location to zero and executes a fun little instruction ud2 which basically means "halt and catch fire" or "lay down to die" if you will. It will generate an invalid op code exception and then close down the shop. Meaning that gcc was smart enough to notice that your program was nonsense and translated it to a similar nonsense program.

This is fully conforming behavior by the compiler. I didn't get any warnings either, since this undefined behavior bug is not violating any constrains or syntax rules of the language.


Since it is Easter: When spotting undefined behavior, older versions of gcc had an "easter egg". They used to look if one out of several old school "rogue-like" games were installed and then upon undefined behavior launched the computer game rogue/nethack etc. Also fully conforming behavior, though I guess the programmers were mildly amused.