checking that `malloc` succeeded in C

42,643

Solution 1

malloc returns a null pointer on failure. So, if what you received isn't null, then it points to a valid block of memory.

Since NULL evaluates to false in an if statement, you can check it in a very straightforward manner:

value = malloc(...);
if(value)
{
    // value isn't null
}
else
{
    // value is null
}

Solution 2

new_list=(vlist)malloc(sizeof (var_list)
if (new_list != NULL) {
  /* succeeded */
} else {
  /* failed */
}

Solution 3

Man page :

If successful, calloc(), malloc(), realloc(), reallocf(), and valloc() functions return a pointer to allocated memory. If there is an error, they return a NULL pointer and set errno to ENOMEM.

Solution 4

The code you have already tests for error, although I normally write the assignment and check as two separate lines:

new_list = malloc(sizeof *new_list);
if (!new_list)
    /* error handling here */;

(Note two small changes - you shouldn't cast the return value, and we take the size from the variable rather than its type to reduce the chance of a mismatch).

If malloc() fails, it returns a null pointer, which is the only pointer value that is false.

The error handling you have is simply return -1; - how you handle that in the calling function is up to you, really.

Share:
42,643
SIMEL
Author by

SIMEL

Updated on October 21, 2020

Comments

  • SIMEL
    SIMEL over 3 years

    I want to allocate memory using malloc and check that it succeeded. something like:

    if (!(new_list=(vlist)malloc(sizeof (var_list))))
      return -1;
    

    how do I check success?

  • Philip
    Philip about 13 years
    Zero can be used wherever NULL is appropriate, but NULL is not zero. It's NULL. It does always evaluate to false, though.
  • Etienne de Martel
    Etienne de Martel about 13 years
    @Phillip My bad, I wrongly assumed that C was like C++ in this regard.
  • Steve Jessop
    Steve Jessop about 13 years
    in C (unlike C++) (void*)0 is a null pointer constant. That's because a void* pointer in C (unlike C++) can be implicitly converted to any other pointer type, so it's possible to allow NULL to have void* type. So in both languages NULL is an implementation-defined null pointer constant, but C implementations have more freedom.
  • Alex D
    Alex D over 7 years
    On some platforms NULL might not be 0 but some addr like 0xFFEE0000 so to be on a safe side and portable I would go with an explicit if (value != NULL)
  • Etienne de Martel
    Etienne de Martel over 7 years
    @Alex NULL is a null pointer constant, and that is guaranteed to evaluate to zero. If your particular implementation doesn't comply with the standard, I feel sorry for you, but on sane platforms you don't need that kind of check.
  • Alex D
    Alex D over 7 years
    @EtiennedeMartel Yep, you are right. The null pointer value sometimes might be other then 0, but it's guaranteed to evaluate to zero.
  • Alex D
    Alex D over 7 years
    @EtiennedeMartel Actually I gave it a thought and now I think I know why I doesn't like it. Readability issue. value is not a boolean expression and I consider it's a bad habit (encapsulated for over 30 years now) write code like if(!value), that's why C++ has an explicit keyword nullptr exactly for that reason; and in other languages like java and c# you cannot write something like if(!value) for a pointer. Even in C, especially in embedded mixing up 0 (zero) and null pointer may lead to some unpleasant side effects.