0

I have a function with a large number of out parameters which currenly looks as

int do_some_foo(char **string_out, int *index_out, char **description_out, etc...);

So since out parameters may grow and currently already about 8 in total (some of them omitted for brevity) I tend to group all the parameters into a struct and document that the caller does not own the out pointers

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);

By this way I think I restricted the called from using this pointer after releasing resources used by the library. So the caller is not supposed to use the pointers after calling to it.

St.Antario
  • 26,175
  • 41
  • 130
  • 318
  • 3
    Why not `int foo_do_some(struct foo_context *ctx, struct foo_out *out)` ? and `void foo_finish(struct foo_context *ctx)` ? – KamilCuk May 12 '19 at 17:14
  • Perhaps do a bit of reading on how to implement opaque data types (aka "*handles*") in C? – alk May 12 '19 at 17:19
  • @alk How would opaque data type help here? Making the `struct foo_out` opaque and providing functions making copy of the strings the definition contains is totally unacceptable from performance standpoint. – St.Antario May 12 '19 at 18:26
  • 2
    Please do not post a couple of code snippets and a long explanation. Rather, post a [mcve] showing what you have tried, how it differs from what you want. Then you will have a question that we can address – user3629249 May 13 '19 at 04:22

0 Answers0