I'm trying to convince (citing the specific parts of the C99 standard) a colleague that the following is undefined behavior:
int *p = malloc(1);
p[0] = 0;
But I cannot find the specific parts in the standard which clearly ensure that this is undefined. I'm looking specifically for the logical steps in the standard which lead from these lines to the conclusion: undefined behavior. Is it the conversion from void *
to int *
in the first line? The assignment in the second line?
The only relevant part I can find about malloc
is that it returns a suitably aligned pointer (7.20.3):
The pointer returned if the allocation succeeds is suitably aligned so that it may be assigned to a pointer to any type of object and then used to access such an object or an array of such objects in the space allocated (...)
I tried grepping for space in the norm, but there's too much noise due to white space and other lexical issues.