ok, i will bite: what is the problem with it ?
ok, i will bite: what is the problem with it ?
As for why shared global mutable state is (generally) bad, see: https://softwareengineering.stackexchange.com/questions/1481...
`man 3 errno` on my Linux system even has a note calling out a common failure pattern. Can you spot the problem?
if (somecall() == -1) {
printf("somecall() failed\n");
if (errno == ...) { ... }
}Returning a"Result" struct doubles the size. This is one less register to use.
Exception handling is even more invasive.
They are great for high(-er) level language, but less prefect on lower level where performance is critical.
EDIT: Linux kernel use negative return value for error. It's good and efficient when it work. But it is not always an option when you need the full register width
Linux even shows you a path, yet you reject it for reasons that don't seem compelling to me.
One less register right before and after returns doesn't sound like a big problem, especially with 16+ registers.
if (somecall() == -1) {
printf("somecall() failed\n");
if (errno == ...) { ... }
}
sure, the issue is that `somecall(...)` might have altered `errno` through 'acts-of-omission-or-comission' :o)fwiw, posix has updated its definition to pretty much say that 'value of errno in one thread is not affected by assignments to it by another'. this has been the case since at least a decade-and-a-half (iirc), which in internet years would positively be in the pleistocenic era :o)
so, i am not sure i really appreciate 'the shared-global-mutable-state' argument above. thanks !
my typical usage for such scenarios i.e. when i know that callee might alter errno etc. is to
int save_errno = errno;
do_foo(...);
if (errno == ...) {
...
}
wrapping libc into something that (maybe) does better seems like such a sisyphean task to me.Notice how this entire thread was started by someone asking why errno was problematic. This is just about understanding.
yes, you are absolutely right. this is just about understanding and how easy it is to miss what is hidden just one-level away.
Linus Torvalds on errno: https://yarchive.net/comp/linux/errno.html
yes, he argues against its usage in the KERNEL.