0

I'v come across an C interview question. The question claims that the following code segfaults on ia64 but not on ia32. I don't think so. I also tested on an 64 bit Linux machine with gcc and it didn't crash at all.

int main()
{
    int* p;
    p = (int*)malloc(sizeof(int));
    *p = 10;
    return 0;
}

What was the intended puzzle?

lang2
  • 11,433
  • 18
  • 83
  • 133
  • 1
    did he include any header? wrong malloc called i believe(hence UB) – Koushik Shetty Jan 10 '14 at 15:56
  • My best guess is that the code is non-portable. If the code is compiled on an ia32 and attempts to run on an ia64 the segfault may occur. – YoungJohn Jan 10 '14 at 15:56
  • ia64 != intel64. The latter being x86_64. ia64 is intel Itanium. – Sergey L. Jan 10 '14 at 15:58
  • See this link http://stackoverflow.com/questions/10197242/what-should-be-the-sizeofint-on-a-64-bit-machine . sizeof(int) may not equal sizeof(int*). – OldProgrammer Jan 10 '14 at 16:01
  • 1
    It's supposed to be about unaligned memory returned from `malloc()`. If the address is not divisible by `sizeof(int)` then this will fail on ia64. But I don't think any sane malloc would return unaligned memory. – Ben Jan 10 '14 at 16:48

0 Answers0