Clang is too clever and gives malloc the correct implicit declaration even when you don't have an explicit prototype:
test.c:10:12: warning: implicitly declaring library function 'malloc' with type 'void *(unsigned long)'
[-Wimplicit-function-declaration]
I think that having a prototype mismatch with the actual declared function is undefined behavior, so this is a legal way to resolve it, but not every compiler will. Older compilers tended to treat malloc as just another function and wouldn't do this.
I was able to replicate the older behavior by wrapping malloc in my own function. In one file:
int main()
{
int* p;
p = (int*)wrapped_malloc(sizeof(int));
*p = 10;
return 0;
}
Compiling them into one program results in a crash. The relevant bit of assembly is:
0000000100000f4f movslq %eax, %rdi
So it is indeed only extracting the lower 32 bits of the returned pointer.
The page looks pretty old (the IA-64 reference sure is dated, anyway) so I'd guess that it was referring to an older compiler that didn't have a special case for malloc like this.
Right, C compilers will automatically known it's part of libc. You didn't need to do the separate file thing, you could just have the wrapper and it will trigger the movslq, or a cltq.
I was able to replicate the older behavior by wrapping malloc in my own function. In one file:
And in another file: Compiling them into one program results in a crash. The relevant bit of assembly is: So it is indeed only extracting the lower 32 bits of the returned pointer.The page looks pretty old (the IA-64 reference sure is dated, anyway) so I'd guess that it was referring to an older compiler that didn't have a special case for malloc like this.