警告:忽略'realloc'的返回值,使用属性warn_unused_result声明

时间:2016-02-04 00:21:32

标签: c warnings compiler-warnings

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

  

警告:忽略'realloc'的返回值,使用属性warn_unused_result声明[-Wunused-result] realloc(strp-> data,nbytes);

                        ^

它想要'警告'我的线的相关代码:

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

提前致谢

1 个答案:

答案 0 :(得分:5)

调用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;