CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2006
    Posts
    384

    Understanding of errno

    Hi,

    Just wanted to clarify my understanding on the global errno variable used in C/C++. The code is executed on Solaris

    Code:
    printf("%d\n",errno);
    func1();
    printf("%d\n",errno);
    In the code snippet above, func1() execution results in an error.
    The output above is indicated as :
    0
    2

    Code:
    printf("%d\n",errno);
    func2();
    printf("%d\n",errno);
    In the code snippet above, func2() execution results in an error.
    The output above is indicated as :
    0
    22

    Code:
    printf("%d\n",errno);
    func1();
    printf("%d\n",errno);
    func2()
    printf("%d\n",errno);
    In the code snippet above, I have combined func1() and func2(), and the output is indicated as below.
    0
    2
    2

    Looks like the errno variable does not get set again to 22 , once it has been set to 2 after func1().

    What do think will need to be done such that in the scenario above, I am able to read errno as 22 in the sequence indicated above ? Resetting errno to 0 (errno = 0) after the second printf() does not seem to help.

    Can you also kindly explain the above behaviour ?

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Understanding of errno

    errno is a remnant of C. It shouldn't be used more than necessary in C++. While it *is* thread-local and thus multithreading won't affect it significantly, there are still better methods of error handling in C++.

    In general, functions only modify errno if they have an error. You can manually reset it to 0 after detecting an error if you like.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Understanding of errno

    Quote Originally Posted by humble_learner View Post
    Hi,

    Just wanted to clarify my understanding on the global errno variable used in C/C++. The code is executed on Solaris
    To add, your three programs are different. They call func1(), func2() and func3() in different combinations, and we don't know what side effects these functions have on each other.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Jan 2006
    Posts
    384

    Re: Understanding of errno

    func1() and func2() are not related.

    Basically, func1() by itself does something which sets the errno to 2, and func2() by itself does something that sets the errno to 22.
    But when func1() and func2() are called in sequence, I was expecting the errno at the end of the program to indicate '22'.

    I would imagine that the errno would be overridden with the latest error occuring in the sequence - but that does not seem to be the case in the sequence above.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Understanding of errno

    Quote Originally Posted by humble_learner View Post
    func1() and func2() are not related.

    Basically, func1() by itself does something which sets the errno to 2, and func2() by itself does something that sets the errno to 22.
    But when func1() and func2() are called in sequence, I was expecting the errno at the end of the program to indicate '22'.
    The programs are different, so unless you have full control of errno, expect different results.

    You see it for yourself right there -- you expected "x", but instead you got "y". It isn't a case of where you have total control over the errno global variable -- if you did have control, and the variable mysteriously changed on you, then it would be strange.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Understanding of errno

    In C++ we tend to catch exceptions and only use errno for C functions that don't throw exception like the math library.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured