gpt4 book ai didi

c - 使用属性 warn_unused_result 声明的警告 : ignoring return value of ‘realloc’ ,

转载 作者:太空宇宙 更新时间:2023-11-04 05:52:49 25 4
gpt4 key购买 nike

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

warning: ignoring return value of ‘realloc’, declared with attribute 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);
}

提前致谢

最佳答案

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

关于c - 使用属性 warn_unused_result 声明的警告 : ignoring return value of ‘realloc’ ,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35190326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com