Thread: reasons why malloc fails?

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    12

    reasons why malloc fails?

    hi,

    will someone point me the reasons why a call to malloc fails even if we have enough memory ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    http://cboard.cprogramming.com/showthread.php?t=77623

    Or maybe you're using crusty old turbo-c which is limited to the 640K of original DOS memory, and has no idea about the other GB the rest of your machine knows about, or that any decent new compiler can access freely.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User joed's Avatar
    Join Date
    Mar 2004
    Posts
    59
    There can be many reasons, but usually the pointer wasn't NULL before using malloc. Do a couple of checks like this:

    Code:
    // example pointer
    int *p;
    
    // free dangling pointer
    if(p)
        free(p);
    
    // ensure pointer is NULL before using malloc
    if(!p)
        malloc...
    
    // check it again to make sure it worked
    // before doing anything with it
    if(!p)
        error...

  4. #4
    Sys.os_type="Unix";;
    Join Date
    Aug 2005
    Posts
    52
    Quote Originally Posted by joed
    There can be many reasons, but usually the pointer wasn't NULL before using malloc. Do a couple of checks like this:

    Code:
    // example pointer
    int *p;
    
    // free dangling pointer
    if(p)
        free(p);
    
    // ensure pointer is NULL before using malloc
    if(!p)
        malloc...
    
    // check it again to make sure it worked
    // before doing anything with it
    if(!p)
        error...

    I don't see why using a pointer that's already pointing to allocated memory to store the return value of a new malloc call would cause malloc to fail.
    Granted it's not a good thing since you'll effectively lose track of your original memory and cause a memory leak if the OS your on isn't smart enough to recover properly.
    It shouldn't cause malloc to fail however although it would be nice if it did.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Actually it would be pretty crappy if it did.
    Code:
    char *foo = malloc( 100 );
    The above code would cause a malloc failure by your definition.


    Quzah.
    Hope is the first step on the road to disappointment.

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. Wierd Malloc Problem
    By mohankarthik in forum C Programming
    Replies: 11
    Last Post: 09-17-2008, 02:14 PM
  3. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  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, 09:08 AM