在C,从职能中解放记忆的最好做法是:
- Provide a "destructor" function that encapsulates the call to free().
- Require users to free() the returned pointer themselves.
例如,我们开设并关闭档案:
FILE* f = fopen("blah", "w");
fclose(f);
最好是:
FILE* f = fopen("blah", "w");
fclose(f);
free(f);
<>strong>Warning: 免费拨打国际能源论坛协调人。 我只在这里使用一种假设性的实施。
当地变数与回来记忆有何关联? 这里是否是免费的? (或也许永远不应这样做)
FILE f = &fopen("blah", "w");
fclose(&f);