how to clean errno?

This is a discussion on how to clean errno? within the C Programming forums, part of the General Programming Boards category; Hi! Is there a way to clean the value in errno? I'm using perror to try to figure out if ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    101

    Question how to clean errno?

    Hi!

    Is there a way to clean the value in errno?

    I'm using perror to try to figure out if something went wrong in a function but I'm pretty sure I'm getting a value that was just there before. How to check if a function changed the value of errno?

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    Just set it to zero before calling a function which might set it.

    Code:
    errno = 0;
    Make sure you are including errno.h
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    101
    Dumb question, thanks for the answer.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Normally you should only check errno if something went wrong... like a function returned:

    fopen() returned a NULL
    fwrite() returned a count written which was not what was intended
    rename() returned non zero
    mkdir() return non zero

    ... and deal with the consequences right then. Not after a function was completed. The way I do it anyhow.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    Quote Originally Posted by nonoob View Post
    Normally you should only check errno if something went wrong... like a function returned:

    fopen() returned a NULL
    fwrite() returned a count written which was not what was intended
    rename() returned non zero
    mkdir() return non zero

    ... and deal with the consequences right then. Not after a function was completed. The way I do it anyhow.
    There are some library functions where negative (or NULL) return values are permitted, and are not errors. The only way to check in that case whether an error occurred is to examine errno (first setting it to zero before calling the function).
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    I don't agree. Granted there are various return codes allowable depending on the specific function. You have to look it up and treat each accordingly.

    I would only check errno in an error case. I can see you method of always setting errno = 0 and then checking it after the function call. Maybe that's cleaner and more consistent ultimately. But I've never seen it done that way.

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    Quote Originally Posted by nonoob View Post
    I would only check errno in an error case. I can see you method of always setting errno = 0 and then checking it after the function call. Maybe that's cleaner and more consistent ultimately. But I've never seen it done that way.
    That's not what I'm saying. For functions which can report errors unambiguously, you should depend on the return value to detect an error, not errno.

    Off the top of my head I cannot remember which specific functions are the weird ones, but at least in the Linux manual pages, the manual specifically says that to check for error, you should clear errno and then check it after the call. If it pops to mind, I'll post it.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    Here is at least one example. Read the stuff under "Rationale":

    readdir_r(3): read directory - Linux man page
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    101
    Just for info, what I was trying to do was finding what went wrong using textures with openGL.

    The methods returned void so there was no other way to check if there was an error. Right?

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    For OpenGL, you don't use errno, but GetLastError().

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    PS: I think setting errno is not really portable, since some (many?) libraries define a function to return errno for the specific thread as errno (#define error _get_thread_errno()) And you can't assign to a function call.

  12. #12
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Quote Originally Posted by Brafil View Post
    PS: I think setting errno is not really portable, since some (many?) libraries define a function to return errno for the specific thread as errno (#define error _get_thread_errno()) And you can't assign to a function call.
    I think the standard requires errno to be a valid lvalue.

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  13. #13
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Well, I tried reading it but got bored quickly ;-p. Now I see, it says "The symbol errno, defined by including the <errno.h> header, expands to a modifiable lvalue of type int". Dunno where I got this from. But how about the thread-specific one?

  14. #14
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by Brafil View Post
    PS: I think setting errno is not really portable, since some (many?) libraries define a function to return errno for the specific thread as errno (#define error _get_thread_errno()) And you can't assign to a function call.
    I side with brewbuck because in order to explicitly test the value of errno, it needs to be set to zero before making any function calls.

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by brewbuck View Post
    at least in the Linux manual pages, the manual specifically says that to check for error, you should clear errno and then check it after the call. If it pops to mind, I'll post it.
    This is only half true. If you know there is an error and you want to know what it is, you do not need to set errno to zero first. Errno will reflect the last actual error that occurred; it does not report "no error".

    For example, with the readdir example, you would only check errno if readdir returns NULL -- in which case errno will be set correctly without having been zeroed. If readdir does not return NULL, then there was no error. I am sure almost all usage conforms to this pattern, so generally speaking there is no reason to set errno to zero.

    If you think perror is returning a previous error, that means there have been any more since then, ie, the function did not set errno at all, and the only thing setting it to zero will demonstrate is that errno is still zero.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Establishing 'make clean' with GNU make
    By Jesdisciple in forum C Programming
    Replies: 9
    Last Post: 04-11-2009, 09:10 AM
  2. errno function, error TLS/non TLS
    By fran.b in forum C Programming
    Replies: 13
    Last Post: 03-25-2009, 08:06 AM
  3. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  4. make clean
    By Jaguar in forum Linux Programming
    Replies: 5
    Last Post: 12-27-2002, 05:44 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

Tags for this Thread


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