7

I can't access my original account. Moderators are requested to merge the accounts if possible.
Here is my question. 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?

Community
  • 1
  • 1
  • 2
    @wacko__Cracko: IA-64? Really? You're using Itanium? Or did you mean EMT64/AMD64? – user541686 Mar 22 '11 at 05:18
  • 1
    A pretty bad interview question none the less. – Steve-o Mar 22 '11 at 05:20
  • 2
    never, ever cast return from `malloc` in C. If this produces an error, you are missing the prototype. – Jens Gustedt Mar 22 '11 at 06:51
  • This question was pulled straight out of http://www.gowrikumar.com/c/ – Nicolás Mar 10 '13 at 00:53
  • This is an exact duplicate of http://stackoverflow.com/questions/7545365/why-does-this-code-segfault-on-64-bit-architecture-but-work-fine-on-32-bit?rq=1 which has 58 votes, although it is a later question so really *it* should be the duplicate. – Zan Lynx Jan 17 '14 at 21:08
  • Possible duplicate of [Why does this code segfault on 64-bit architecture but work fine on 32-bit?](https://stackoverflow.com/questions/7545365/why-does-this-code-segfault-on-64-bit-architecture-but-work-fine-on-32-bit) – hat May 31 '18 at 12:43

3 Answers3

8

In C the default return type is int if the function is not prototyped. In ia64 the size of a pointer is larger than an int and so it can segfault.

Update: The question is basically why you should always prototype your functions (or include the appropriate headers for that matter).

ChrisWue
  • 18,612
  • 4
  • 58
  • 83
  • For added confusion you can configure an IA64 to use ILP64 and hence `sizeof(int) == sizeof(int*)`. – Steve-o Mar 22 '11 at 05:36
  • Yeah, although I think under most Unix/Linux systems and Windows in 64bit `sizeof(int*)` is 64 bit while `sizeof(int)` is 32 bit. I guess there is more software around relying on the fact that `sizeof(int) == 4` than software which assumes `sizeof(int) == sizeof(int*)` – ChrisWue Mar 23 '11 at 01:20
2

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.

Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
1

As it's IA64 (itanic) and not x64 it's probably something basic like malloc not guaranteeing alignment, cf. memalign and early versions of IA64 don't support unaligned memory access.

Steve-o
  • 12,678
  • 2
  • 41
  • 60
  • 2
    But malloc _does_ guarantee alignment for the basic data types. – paxdiablo Mar 22 '11 at 05:20
  • Isn't the alignment supposed to be 4 bytes, though, if you're accessing a 4-byte integer? – user541686 Mar 22 '11 at 05:21
  • `glibc` returns 8-byte aligned addresses other libraries don't, for example HP/UX on IA64. – Steve-o Mar 22 '11 at 05:23
  • OK HP/UX defaults to 16-byte alignment but you can set it to 1-byte alignment and get the SEGFAULT: http://docstore.mik.ua/manuals/hp-ux/en/B2355-60130/malloc.3C.html – Steve-o Mar 22 '11 at 05:31