warning: ignoring return value of ‘realloc’, decla

2019-09-09 20:14发布

I'm curious, I'm programming in C on PuTTy, does anyone know how I can get rid of this warning?

warning: ignoring return value of ‘realloc’, declared with attribute warn_unused_result [-Wunused-result] realloc(strp->data, nbytes);

                        ^

Relevant code to the line it wants to 'warn' me about:

         //If the previously allocated size is > 0 then we can reallocate
         //otherwise we have to make a new allocation in memory
         if(strp->length > 0)
         {
           realloc(strp->data, nbytes);
         }
         else
         {
           *strp = kstralloc(nbytes);
         }

Thanks in advance

1条回答
Summer. ? 凉城
2楼-- · 2019-09-09 21:17

The correct way to call realloc is something like this:

tmp = realloc(strp->data, nbytes);
if (tmp == NULL) {
    // your realloc didn't work and strp->data still points to the
    // the original location
    return EMEMORY;
}
strp->data = tmp;
查看更多
登录 后发表回答