The following C program segfaults of IA-64, but works fine on IA-32.

185 views
Skip to first unread message

c is intresting

unread,
Sep 28, 2007, 12:39:45 AM9/28/07
to Programming Puzzles
The following C program segfaults of IA-64, but works fine on IA-32.

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,

CH Gowri Kumar - గౌరీ కుమార్

unread,
Sep 28, 2007, 1:10:30 AM9/28/07
to programmi...@googlegroups.com
Hi Vinod,
The reason is : missing prototype for malloc

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.

Reply all
Reply to author
Forward
0 new messages