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?

SIMEL
  • 8,745
  • 28
  • 84
  • 130
  • 3
    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*`. – Steve Jessop Apr 09 '11 at 20:10

4 Answers4

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
}
Etienne de Martel
  • 34,692
  • 8
  • 91
  • 111
  • 3
    Zero can be used wherever NULL is appropriate, but NULL is not zero. It's NULL. It does always evaluate to false, though. – Philip Apr 09 '11 at 23:38
  • @Phillip My bad, I wrongly assumed that C was like C++ in this regard. – Etienne de Martel Apr 10 '11 at 19:00
  • 2
    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. – Steve Jessop Apr 12 '11 at 10:39
  • 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) – Alex D Jan 26 '17 at 00:24
  • 1
    @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. – Etienne de Martel Jan 26 '17 at 05:13
  • @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 Jan 26 '17 at 05:26
  • @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. – Alex D Jan 26 '17 at 17:38
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.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Spyros
  • 46,820
  • 25
  • 86
  • 129
8
new_list=(vlist)malloc(sizeof (var_list)
if (new_list != NULL) {
  /* succeeded */
} else {
  /* failed */
}
jdehaan
  • 19,700
  • 6
  • 57
  • 97
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.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103