Return value: Error or result?

This is a discussion on Return value: Error or result? within the C Programming forums, part of the General Programming Boards category; I have several functions that suffer this issue, and I'm having a hard time making a design decision. The function(s) ...

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

    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...)
    Code:
    // 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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    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
    Posts
    5,006
    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
    Location
    Toronto
    Posts
    591
    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, 01: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, 09:01 PM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 12:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21