问题
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