Returning error codes from a C function which returns object pointer?

Danijel

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?

Lundin

There are pretty much only two acceptable options:

  1. struct object *object_create (myerr_t* result, /* ... */);
    Return the struct and pass the function result as parameter.

  2. myerr_t object_create (struct object** obj, /* ... */);
    Return the function result and pass a pointer to the struct by parameter.

Pros of 1:

  • It has the advantage that you can assign variables directly on the caller side:
    struct object* obj = object_create(/* ... */);
  • Pointer-to-pointer interfaces add a slight bit of complexity which can be avoided this way.

Pros of 2:

  • Reserving the return value of a function for an error code is an industry "de facto" standard way of designing APIs/libs. It is common to to use the same result type for the whole lib and have every function return it.
  • You have the option not to modify the struct in case of errors, leaving the caller's variable intact. Mainly matters in case of "realloc-like" interfaces.

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.

edited at
0

Comments

0 comments
Login to comment

Related

Calling function in C dll from C# which returns somekind of pointer to function pointer

Returning anonymous JS function which returns javascript plain object from Dart

Returning NULL value from a function to a pointer in C

Returning error codes from a method

C returning const char pointer or char pointer from a function

Function which returns list of struct from gorm database returning nil?

Call a C function that returns a pointer from cython

Implement a function which returns a pointer

Runtime error while returning dereferenced pointer's value from a function in C

Returning an object pointer from a recursive function met with access violation

Is it legal to cast a function returning an object pointer to a function returning a void pointer?

Returning an object from a function in C++

Returning a pointer to a structure from function

Returning a pointer to structure from a function

returning a vector pointer from a function

Cannot resolve the errors with returning an array using pointer from a function in C?

Returning an array address from a function to a pointer (C++)

Dynamically allocated pointer to a structure not returning from C function

Pointer not returning after function in C

Returning a Function Pointer in C++

Do I need to AddRef() if returning pointer to pointer COM object from function?

Describe typescript function which returns modified object with keys from array

How to get the value of a property from a function which returns an object?

Returning from a function returns undefined

Returning values through the arguments of go function, which is called from C

Is a function call returning a pointer to an object a prvalue?

Print pointer string which is return from function in C

Returning a function pointer from another function

Defining a function which returns a function pointer which also returns a function pointer without typedefs