c - 将参数分组到结构中
问题描述
我有一个带有大量输出参数的函数,目前看起来像
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);
通过这种方式,我想我在释放库使用的资源后限制了被调用者使用这个指针。所以调用者在调用它之后不应该使用指针。
解决方案
推荐阅读
- node.js - 节点js amqplib,消费者缓存中的回调来自父函数的参数
- c - dbus 信号中收到的消息与发送的数据不匹配
- javascript - 为什么我的图像和信息不能在我的 React localhost 上呈现?
- react-native - 适用于 React Native 的 FreshChat SDK:横向模式
- web-component - 可以请求另一个 HTML URL 并将其注入它的影子 DOM 的 Web 组件
- sql - 日期函数的问题
- api - 将带有文件的 Postman 表单数据请求转换为 .NET Core API 的 cURL
- c++ - Xcode 13 无法在 Rosetta 下编译 SFML c++ 程序
- hasura - Hasura 迁移存储在 DB 中的什么位置?
- javascript - 检查表格而不提交?