This is the signature of my object create function:
struct object *object_create();
If the object is successfully created it it returns pointer to the object, and 0
otherwise.
How would I return some error codes from the same function? I need about ten codes.
I suppose mixing error codes and object pointer is not the way to go?
There are pretty much only two acceptable options:
struct object *object_create (myerr_t* result, /* ... */);
Return the struct and pass the function result as parameter.
myerr_t object_create (struct object** obj, /* ... */);
Return the function result and pass a pointer to the struct by parameter.
Pros of 1:
struct object* obj = object_create(/* ... */);
Pros of 2:
I would not advise anyone to use errno
because using a global error result variable is obscure, error prone, severely restricted and (pre-C11) not thread-safe. Also it might conflict with skunky, legacy error handling by various standard/POSIX lib functions.
I would not advise to use any wrapper hacks either, such as wrapping the returned struct in a larger one or over-allocating an error code at the end. This just adds complexity and potential for bugs.
An error handler should be simple, not introduce potential errors of its own due to added complexity! Anything else but a plain enum result code is questionable practice.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments