Pointer issue on IA-32 and IA-64

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Aftabpasha
    New Member
    • Oct 2008
    • 32

    Pointer issue on IA-32 and IA-64

    I've seen a puzzle while surfing. It is given below.
    The following C program segfaults of IA-64, but works fine on IA-32.
    Code:
      int main()
      {
          int* p;
          p = (int*)malloc(sizeof(int));
          *p = 10;
          return 0;
      }
    Why does it happen so?
    I think, the problem is related to pointer type conversion. I don't have a 64-bit processor. I was unable to fully elaborate it.
    Please explain why does this program behaves differently on different architectures.
    Thanks.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by Aftabpasha
    I've seen a puzzle while surfing. It is given below.

    I think, the problem is related to pointer type conversion. I don't have a 64-bit processor. I was unable to fully elaborate it.
    Please explain why does this program behaves differently on different architectures.
    Thanks.
    If that code snippet results in a seg-fault the malloc implementation is broken.

    kind regards,

    Jos

    Comment

    • Aftabpasha
      New Member
      • Oct 2008
      • 32

      #3
      But the malloc() just allocates a BLOCK OF MEMORY of specified size and returns a void pointer to it's starting address. That's why I thought the problem is some where in pointer conversion process. Just don't know where exactly it is.
      The following is the link to the puzzle


      I just saw an old discussion on the same puzzle, but I ended up with confusions. Here is the link to it
      สล็อตเว็บตรง API แท้ ไม่ผ่านเอเย่นต์ เว็บสล็อตใหม่ล่าสุด ฝากถอนง่าย ผ่านทุกธนาคารในไทย เว็บตรง 100% เบทน้อยเล่นได้ ทุกที่ทุกเวลา


      Please explain it.
      Thanks.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by Aftabpasha
        But the malloc() just allocates a BLOCK OF MEMORY of specified size and returns a void pointer to it's starting address. That's why I thought the problem is some where in pointer conversion process. Just don't know where exactly it is.
        Not just any pointer value: malloc returns a pointer value that is suitably aligned for any data type (usually a multiple of 8 or 16).

        kind regards,

        Jos

        ps. found it: malloc() isn't prototyped so the compiler assumes an int return value. If a pointer happens to be larger than an int disaster will happen.

        Comment

        • Aftabpasha
          New Member
          • Oct 2008
          • 32

          #5
          I don't think protottyping is a reason. If proper header file for malloc() is not included, then compiler would show an error message rather than trying to return an integer (default return type).

          I think Jos is right. I guess it is faulty malloc implementation that is triggering this problem. I think the things are going as described below.

          According to the C standard, malloc() always returns a suitably aligned address. It is essential because the address (of type void *) can to be further converted to other other types (e.g. int *).

          I think, in the above problem, probably the malloc() (or faulty implementation of malloc) is not giving a suitably aligned address on IA-64. Eventually this resulted in improper pointer conversion from void * to int *. On an IA-64 (x86) machine, which is of type Little Endian, this conversion might have pointed pointer p to some restricted address. Hence generating a segmentation fault.

          I'm not sure about the above explanation. But thats what I can guess.
          Please have some comment on it.

          Thanks and kind regards.

          -Aftab

          Comment

          • newb16
            Contributor
            • Jul 2008
            • 687

            #6
            C ( not C++) compiler may issue a warning but not an error. More surfing proves that the root cause is indeed missing prototype.

            Comment

            • unauthorized
              New Member
              • May 2009
              • 81

              #7
              Your problem is here:
              p = (int*)malloc(si zeof(int));

              You sizeof(int) is 4 bytes in most compilers. What you need is sizeof(int*).

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                That is completely wrong. sizeof(int) is correct as the code is trying to allocate memory to hold an int not enough memory to hold an int *.

                On a 64bit system it is reasonable to assume that the point will be 64bits. If the prototype isn't there then the compiler will assume that malloc returns int which is normally 32bits on modern 64bit systems so only 1/2 the bits of the pointer get copied and the error occurs. This doesn't happen on a 32bit system because generally both int and pointer have 32bits so assuming malloc returns int is not so disastrous.

                Comment

                • Aftabpasha
                  New Member
                  • Oct 2008
                  • 32

                  #9
                  I completely missed the 'only C' perspective of the problem. Now all doubts are cleared.
                  Thank you all for their quick attention and helpful comments.

                  Regards,

                  Aftab.

                  Comment

                  • Surendra Gurjar
                    New Member
                    • Feb 2012
                    • 1

                    #10
                    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.

                    Comment

                    Working...