C code 32 bit vs 64 bit

Source: http://www.gowrikumar.com

Problem:
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;
  }
 
Update (Oct 30, 2011):
Wrong problem. Sorry for the trouble.

Comments

  1. int* is 64 bits on a 64 bit machine, but 32 bits on a 32 bit machine. int, on the other hand, is 32 bits on both. On a 32 bit machine, this would work fine, but on a 64 bit machine it segfaults, since you are allocating 32 bits of memory, but accessing 64.
    Cheers...

    ReplyDelete
  2. @ravi :- doesn't matter. p is pointing to a 32 bit value... shouldn't segfault.
    the above code segfaulting makes no sense whatsoever.
    doesn't segfault for me at least (as in fact it shouldn't. ) i'm guessing someone used a crappy compiler that got confused as ravi points out.
    pratik, can you post the original link?

    ReplyDelete
  3. I too think that Ravi's solution is wrong. It should not segfault.

    You have this problem somewhere in the middle of the page http://www.gowrikumar.com/c/index.html

    ReplyDelete

Post a Comment

Popular posts from this blog

Buying Dimsums

Fraction Brainteaser

Consecutive Heads