警告:忽略 return ‘realloc’ 的值,用属性 warn_unused_result 声明

warning: ignoring return value of ‘realloc’, declared with attribute warn_unused_result

我很好奇,我在 PuTTy 上用 C 编程,有人知道我怎样才能摆脱这个警告吗?

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

                        ^

相关代码到行就是了'warn'我一下:

         //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);
         }

提前致谢

调用realloc的正确方法是这样的:

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;