checking that `malloc` succeeded in C
Asked Answered
R

4

15

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?

Rich answered 9/4, 2011 at 19:50 Comment(1)
Your code already does check success. On failure it returns -1, on success it carries on to the next line. I hope vlist is a typedef for var_list*.Flavius
H
31

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
}
Heartland answered 9/4, 2011 at 19:51 Comment(7)
Zero can be used wherever NULL is appropriate, but NULL is not zero. It's NULL. It does always evaluate to false, though.Leniency
@Phillip My bad, I wrongly assumed that C was like C++ in this regard.Heartland
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.Flavius
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)Circular
@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.Heartland
@EtiennedeMartel Yep, you are right. The null pointer value sometimes might be other then 0, but it's guaranteed to evaluate to zero.Circular
@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.Circular
G
8

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.

Gavel answered 9/4, 2011 at 19:52 Comment(0)
P
8
new_list=(vlist)malloc(sizeof (var_list)
if (new_list != NULL) {
  /* succeeded */
} else {
  /* failed */
}
Pomeranian answered 9/4, 2011 at 19:53 Comment(0)
N
1

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.

Neurosurgery answered 20/10, 2016 at 12:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.