警告:忽略使用属性warn_unused_result声明的“ realloc”返回值 [英] warning: ignoring return value of ‘realloc’, declared with attribute warn_unused_result

查看:101
本文介绍了警告:忽略使用属性warn_unused_result声明的“ realloc”返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇,我在PuTTy上的 C 中编程,有人知道我该如何摆脱这种警告?

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


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

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

预先感谢

推荐答案

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

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;

这篇关于警告:忽略使用属性warn_unused_result声明的“ realloc”返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆