4 ms·
The only time a function would "silently" return a -1 would be if you're not checking your returns for errors! Functions have return codes for a reason.
by amackera 18y ago
The only time a function would "silently" return a -1 would be if you're not checking your returns for errors! Functions have return codes for a reason.
- Hexstream 18y agoBut oftentimes I want a correct result or an obvious failure, without having to handle tons of errors I don't even want to account for.
- holygoat 18y agoReturning -1 is an obvious failure. What you're saying is that you want your programs to blow up when a function returns a known error (such as "disk full"). One of the points of the article is that C makes you think about all the possible, well-documented, errors that each function can encounter. You know when you write C precisely what can go wrong at each stage, and decide the right way to handle it. Seeing a dialog box pop up with some exception backtrace is one of the things that annoys me about 'modern' Java-esque languages: they seem to encourage developers to default to ignoring most exceptions.
- aaronblohowiak 18y agoDefensive programming vs. `Let it fail`. I think the space between to two is more uncomfortable than either extreme.
- wheels 18y agoInterestingly, I noticed earlier this week that the new GCC gives warnings when the return values of POSIX / libc functions are ignored but shouldn't be.
- maw 18y agoWhich versions of gcc and libc? Any special flags passed to gcc? I ask because I just tried it with a test program here (openSUSE Factory, gcc 4.3.3 and glibc 2.8.90) and it didn't print any warnings, but it would be nice if it did.
- janm 18y agoThe difference between letting an exception bubble up and seeing a system call fail and aborting is pretty small. Either way, you need a higher level way of dealing with it. One of the most interesting papers I read was "Why computers fail and what we can do about it" by Jim Gray while he was at Tandem. Transactions are essential, and most failures are transient. This can be applied at many levels, and can deal with many failures without the developer of the mainline code having to decide what to do for each given failure.
- etal 18y agoExactly. In properly written C, about half your code is checking for errors and handling them explicitly. If you don't do that, at some point it will blow your foot off; you have to account for those errors. C programmers have generally accepted this and thus spend an excessive amount of coding time thinking about and writing recovery mechanisms for external errors -- and that's why so many C programs are incredibly stable.
- janm 18y agoYes.