こういうコードはしばしば書いた。
FooStruct * initFoo(void)
{
static FooStruct * sFoo;
if (sFoo == NULL) {
sFoo = malloc(sizeof(FooStruct));
}
setSomeFoo(sFoo);
return sFoo;
}
当然、free()してない。
或いはこういうコードも。
FooStruct * allocFoo(unsigned cnt)
{
static FooStruct * sFoo;
if (sFoo) {
free(sFoo);
}
sFoo = malloc(sizeof(FooStruct) * cnt);
setSomeFoos(sFoo, cnt);
return sFoo;
}
やはり、最後はfree()してない。