In general, C does not prevent you from doing incorrect things.
After int *p = malloc(0);
, p
has some value. It might be a null pointer, or it might point to one or more bytes of memory. In either case, you should not use it.1 But the C language does not stop you from doing so.
When you execute *p = 10;
, the compiler may have generated code to write 10 to the place where p
points. p
may be pointing at actual writable memory, so the store instruction may execute without failing. And then you have written 10 to a place in memory where you should not. At this point, the C standard no longer specifies what the behavior of your program is—by writing to an inappropriate place, you have broken the model of how C works.
It is also possible your compiler recognizes that *p = 10;
is incorrect code in this situation and generates something other than the write to memory described above. A good compiler might give you a warning message for this code, but the compiler is not obligated to do this by the C standard, and it can allow your program to break in other ways.
Footnote
1 If malloc
returns a null pointer, you should not write to *p
because it is not pointing to an object. If it returns something else, you should not write to *p
because C 2018 7.22.3.1 says, for this of malloc(0)
, “the returned pointer shall not be used to access an object.”