我有一个带有大量out参数的函数,看起来像这样
int do_some_foo(char **string_out, int *index_out, char **description_out, etc...);
因此,由于out参数可能会增长,目前总共约8个(为简洁起见,其中一些省略了),我倾向于将所有参数归为一个结构,并记录调用者不拥有out指针的
struct foo_out{
int status;
char *string;
int index;
char *description;
//...
};
//Pointers contained in the return value are not owned by
//the caller and should not be modified or reclaimed by it in
//any way
const struct foo_out *do_some_foo(int *error_code);
//Release all the resources used by the library
//all const struct foo_out * pointers returned by do_some_foo
//becomes invalid
void prepare_shutdown(void);
通过这种方式,我认为在释放库使用的资源之后,我限制了被调用者使用此指针。因此,调用方不应在调用后使用指针。