-
November 29th, 2010, 11:14 PM
#1
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 ?
-
November 29th, 2010, 11:41 PM
#2
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.
-
November 29th, 2010, 11:52 PM
#3
Re: Understanding of errno
Originally Posted by humble_learner
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
-
December 5th, 2010, 11:50 PM
#4
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.
-
December 6th, 2010, 01:01 AM
#5
Re: Understanding of errno
Originally Posted by humble_learner
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
-
December 6th, 2010, 08:57 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|