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

谁说我不能喝 提交于 2019-12-13 07:13:51

问题


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:


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;


来源:https://stackoverflow.com/questions/35190326/warning-ignoring-return-value-of-realloc-declared-with-attribute-warn-unused

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!