要编写健壮的C程序,如何避免太多不同的free()组合?

Kun*_* Wu 11 c memory-management

例如,我需要malloc两块内存,所以:

void *a = malloc (1);

if (!a)
  return -1;

void *b = malloc (1);

if (!b)
{
  free (a);
  return -1;
}
Run Code Online (Sandbox Code Playgroud)

请注意,如果第二个malloc失败,我必须首先释放"a".问题是,如果有很多这样的malloc和错误检查,这可能会非常混乱,除非我使用臭名昭着的"goto"子句并仔细安排free的顺序以及标签:

void *a = malloc (1);

if (!a)
  goto X;

void *b = malloc (1);

if (!b)
  goto Y;

return 0; //normal exit

Y:
  free (a);
X:
  return -1;
Run Code Online (Sandbox Code Playgroud)

你对这种情况有什么更好的解决方案吗?提前致谢.

Rol*_*dXu 18

我们喜欢这样:

void *a = NULL;
void *b = NULL;
void *c = NULL;
a = malloc(1);
if (!a) goto errorExit;
b = malloc(1);
if (!b) goto errorExit;
c = malloc(1);
if (!b) goto errorExit;

return 0;
errorExit:
//free a null pointer is safe.
free(a);
free(b);
free(c);
return -1;
Run Code Online (Sandbox Code Playgroud)

  • 用于指出free接受空指针的+1.我不知道是这样的. (4认同)

Zan*_*ynx 13

在我看来,使用goto并不是一件坏事.使用它进行资源清理是恰到好处的.

像Linux内核一样着名的源代码使用该技术.

只是不要使用转到后退.这会导致灾难和混乱.只有向前跳是我的建议.

  • 不仅Linux内核能做到这一点,而且FreeBSD也是如此.完全同意这是正常做法. (4认同)