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

0 c warnings compiler-warnings

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

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

                        ^
Run Code Online (Sandbox Code Playgroud)

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

         //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);
         }
Run Code Online (Sandbox Code Playgroud)

提前致谢

bru*_*ceg 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;
Run Code Online (Sandbox Code Playgroud)

  • ......后跟`strp-> data = tmp` (3认同)