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?
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With