简体   繁体   中英

macro wrapper for malloc pros and cons

I've always done checking what malloc returns like this:

void *p; 
p = malloc(100);
if (p) 
{ 
  perror("malloc"); 
  return false;
}

But one thought, why do it if you can guarantee getting memory?

#define GET_MEM(p,s,i) do{for(p=NULL,i=0;!(p=calloc(1,s))&&i<5;perror("calloc"),i++)}while(0)

What are the pros and cons of such a decision? How should it be done?

why do it if you can guarantee getting memory.

Your assumption is wrong.

malloc() calloc() and realloc() might fail. So when the memory requested by you is not successfully allocated then these functions retrun NULL. If this check is not there then you go ahead and start writing to this memory location which is not allocated which causes undefined behavior.

If your question is about why there is a do while loop in the macro. The below link explains it really well

do { ... } while (0) — what is it good for?

First of all, this is incorrect and actually does the opposite of what it should:

void *p; 
p = malloc(100);
if (p) 
{ 
  perror("malloc"); 
  return false;
}

This if statement says: if( malloc succeeded ){ return AN_ERROR; } if( malloc succeeded ){ return AN_ERROR; }

It should say:

void *p; 
p = malloc(100);
if( NULL == p ) 
{ 
  perror("malloc"); 
  return false;
}

Now as for your question:

But one thought, why do it if you can guarantee getting memory?

As Gopi stated, you cannot guarantee getting memory. You very well can run out of memory and thus be unable to allocate anymore. It is unlikely that you will use up all of the available memory for the process, but it could happen. This is why checking the return of functions like like malloc() , calloc() , and realloc() is essential.

Any of these memory allocation functions can fail if there is not enough memory and you must be prepared to handle that. Let us suppose you allocate space for string, but malloc fails and you do not check for that:

int main(int argc, char *argv[]){
    char stringToCopy[] = "Some very long string would be here ...";
    char *stringBuffer = malloc( 1000 ); // 1000 char buffer
    // Malloc failed and we did not check for it
    strncpy(stringBuffer, stringToCopy); /* UNDEFINED BEHAVIOUR */
    return 0;
}

So in this situation, malloc failed and we proceeded to attempt to copy the string to the buffer anyway. As soon as we begin doing that, we cause undefined behavior. Typically this kind of action would cause the program to crash due to a Segmentation Fault .

This can be serious and can result in the loss of data or the loss of service. At a minimum, a segmentation fault would inconvenience the user.

As some general rules: Always check the return values of malloc() , calloc() and realloc() ; Never blindly trust that these functions were successful.

Whether you choose to do this check inside of a macro or not is your own choice (there aren't many advantages/disadvantages), but just make sure you perform the check and you perform it properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM