B
Billy Mays
I typically check my malloc calls like this:
if( !(pointer = malloc(number)) )
{
fprintf(stderr, "An error!\n");
exit(EXIT_FAILURE);
}
.... or something similar. However, I often see people do this:
pointer = malloc(number);
if( number == NULL )
{
fprintf(stderr, "An error!\n");
exit(EXIT_FAILURE);
}
Is there a proper way to check the return value of a malloc call? Are
there any benefits to doing it one way over another?
Bill
if( !(pointer = malloc(number)) )
{
fprintf(stderr, "An error!\n");
exit(EXIT_FAILURE);
}
.... or something similar. However, I often see people do this:
pointer = malloc(number);
if( number == NULL )
{
fprintf(stderr, "An error!\n");
exit(EXIT_FAILURE);
}
Is there a proper way to check the return value of a malloc call? Are
there any benefits to doing it one way over another?
Bill