Malloc -segfault

This is a discussion on Malloc -segfault within the C Programming forums, part of the General Programming Boards category; The following C program segfaults of IA-64, but works fine on IA-32. Code: int main() { int* p; p = ...

  1. #1
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58

    Malloc -segfault

    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?

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    you don't have to cast malloc explicitly since p is declared as an int pointer I believe the cast is implicit. (I could be wrong on this).

    However your error is because you set *p = 10. You are setting the address of p to 10. You can't do that.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,632
    I suggest trying with a more correct example:
    Code:
    #include <stdlib.h>
    
    int main()
    {
        int* p = malloc(sizeof(*p));
        if (p)
        {
            *p = 10;
        }
        free(p);
        return 0;
    }
    Quote Originally Posted by Bladactania
    However your error is because you set *p = 10. You are setting the address of p to 10. You can't do that.
    No, that assigns the value of 10 to the int pointed to by p, so it is perfectly fine, unless the pointer is a null pointer.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,181
    No, "*p = 10" writes 10 to the memory block pointed to by 10.

    I don't see anything wrong with it (and it does work on my 64-bit Linux).

    BTW, I think you meant x86-64 instead of IA-64 (Intel Itanium).

    How are you running it? Command line or through an IDE? Are you sure you are running the code? (did the program build successfully?)

  5. #5
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Never use malloc() without checking its return value.

    you don't have to cast malloc explicitly since p is declared as an int pointer I believe the cast is implicit. (I could be wrong on this).
    But you are right. malloc() returns void*, which is supposed to implicitly cast to any other pointer type.

    laserlight's call to malloc() is perfect, since it doesn't make any assumptions about the type of p by making clever use of sizeof.

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    void* will implicitly cast to any pointer type, provided the compiler knows you have a void* to begin with; if you do not #include <stdlib.h>, then the compiler has no idea that a void* is involved (since it will assume an implicit int return). Presumably a pointer is different sizes on a 32-bit machine and a 64-bit machine, hence the "not always working" aspect of your code.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    4
    Perhaps the same reason given here

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by mahalakshmi View Post
    Perhaps the same reason given here
    Yeah. What compiler are you using? If you do not include stdlib.h it should warn you -- but in any case that will cause a problem.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MK27 View Post
    Yeah. What compiler are you using? If you do not include stdlib.h it should warn you -- but in any case that will cause a problem.
    No matter what compiler is beign used - if there is no stdlib include, malloc will be seen to return an integer - 32-bits. The cast will make sure the compiler is still happy to convert an integer to a pointer, and will do so by sign-extending the 32-bit value to 64-bit. It will thus give a invalid memory address [unless the first or last 2GB of virtual space is accessible, and if I remember rightly, most Linux distributions explicitly make those spaces UNAVAILABLE for this very reason - catching pointers that have "lost the upper 32 bits"]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. malloc() resulting in a SegFault?!
    By cipher82 in forum C++ Programming
    Replies: 21
    Last Post: 09-18-2008, 11:24 AM
  3. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 11:26 AM
  4. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 08:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21