One of the reasons I could think of is that the prototype of malloc
is missing considering pre 99 compiler.
Implicit int (return type) is deprecated. However if your code segfaults that means the compiler assumes that functions (without any prototype in scope) return integer by default. As a result malloc
would be treated as returning an integer instead of a pointer.
On 32 bit implementations sizeof(int)
and sizeof(void*)
is 32 bits each. On 64 bit implementations sizeof(int)
is still the same but sizeof(void*)
is 64 bits.
Trucation of 64 bits pointer to 32 bits might be causing that problem.
Include <stdlib.h>
to solve the problem.