int main()
{
int* p;
p = (int*)malloc(sizeof(int));
*p = 10;
return 0;
}
Why does it happen so?
The only reason i see is that memory might not have been allocated. p
is having NULL so when we assign some value. It segfaults. Want to
know if there is any other reason,
In C, if the prototype for a function is not provided, the compiler
assumes that the function takes an integer and returns an integer. As
a result malloc would be treated as returning an integer instead of a
pointer.
On 32 -bit systems, both integer and pointer are 32-bits, hence the
program works fine.
(the model is called ILP-32 : Integer, Long, Pointer are 32-bits)
But on Linux/IA-64 with gcc, the default model is LP-64, where int is
32 bits and long,pointer are 64 bits. As a result of it the pointer
gets truncated to 32-bits and hence causes the segfault.
Regards,
Gowri Kumar
ps : I see no point in your fancy name. There is a danger that your
posts would be ignored by a lot of us for being non-interesting.