Thread: Return value: Error or result?

  1. #1
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Banks of the River Styx

    Question Return value: Error or result?

    I have several functions that suffer this issue, and I'm having a hard time making a design decision. The function(s) in question must create datastructures, and return them if successful. Of course, they could fail, and an error code would always be nice. So my choices are thus: (As I see it...)
    // Return error code. (0 = success, nonzero = fail)
    int CreateThing(Thing **result);
    // Use NULL to indicate failure, but lose ability to differentiate between errors
    Thing *CreateThing();
    // Return a thing, but have error code.
    Thing *CreateThing(int *error);
    Any opinions/knowledge of the (dis)advantages of each one? (Which one wins out in other programming projects?) Or am I just overcomplicating things?
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Portland, OR
    First, decide if the problem is imaginary. Are there really many different error codes that could be returned? Or, as is often the case, is the only real error condition an out-of-memory?

    Normally, a function that returns both a status and an object will give the status in the return value, and store the object by reference. This lets you call the function directly in a conditional expression. If the status is returned in a parameter, you have to make the call as its own statement, then check the returned status in a separate statement.

    Another approach is to create a library function which gives a specific error code. When library functions fail, they set this code and return NULL. The caller notices this NULL and then calls the get_error() (or whatever) function to get the specific code.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    I think the standard library does a fine job with #1 and #2, so I might look there for hints and discard #3. [And this really says pretty much "yup" to what brewbuck said.]
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered Abuser
    Join Date
    Jun 2006
    For an easy solution to #2, if you know that your implementation of errno.h provides errno as a modifiable lvalue, then you can just implement your error handling using errno. But if you're looking for something more well-defined, standard and re-usable, I'd go with what brewbuck said and make your own error handling library.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  2. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  3. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  4. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 01:54 AM