Live data from Hacker News

How can C Programs be so Reliable?

tratt.net

11–20 of 105 posts

Re: How can C Programs be so Reliable?

#11

Earlier quoted context omitted.

"As the author alludes to as well - in C you're made more aware of the error conditions you can handle and the ones you can't." You mean like when a function silently returns -1 to indicate failure and then you wonder why your program returned a wrong result (if you're lucky enough to even notice)? In the bigger part of most of my programs I want a big, flashy, loud, total failure by default if anything goes wrong.

You mean like when a function silently returns -1 to indicate failure and then you wonder why your program returned a wrong result (if you're lucky enough to even notice)? This is the fault of the programmer who wrote the function, not of the language.

What? What can a programmer do besides returning a "special" value such as -1 in a language like C with strict types and that doesn't support exceptions?

Re: How can C Programs be so Reliable?

#12
post #8

Earlier quoted context omitted.

"As the author alludes to as well - in C you're made more aware of the error conditions you can handle and the ones you can't." You mean like when a function silently returns -1 to indicate failure and then you wonder why your program returned a wrong result (if you're lucky enough to even notice)? In the bigger part of most of my programs I want a big, flashy, loud, total failure by default if anything goes wrong.

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 .

But 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.

Re: How can C Programs be so Reliable?

#13

Earlier quoted context omitted.

"As the author alludes to as well - in C you're made more aware of the error conditions you can handle and the ones you can't." You mean like when a function silently returns -1 to indicate failure and then you wonder why your program returned a wrong result (if you're lucky enough to even notice)? In the bigger part of most of my programs I want a big, flashy, loud, total failure by default if anything goes wrong.

if ((result = foo()) == -1) { fprintf(stderr, "!$*!$&^!$*!!!!\n"); exit(1); }

That won't scale. I'm sure you'll have lots of fun grepping through your source files looking for "!$!$&^!$!!!!" when it appears on your stderr. The point is that with an exception system you don't have to write stupid shit like that everywhere just to crash and burn correctly.

Failing loudly is the only sensible behavior when an unexpected error occurs and there's absolutely no indication of what to do in such a case.

Re: How can C Programs be so Reliable?

#14

Earlier quoted context omitted.

You mean like when a function silently returns -1 to indicate failure and then you wonder why your program returned a wrong result (if you're lucky enough to even notice)? This is the fault of the programmer who wrote the function, not of the language.

What? What can a programmer do besides returning a "special" value such as -1 in a language like C with strict types and that doesn't support exceptions?

You don't need an exception mechanism to deal with this - good libraries will let you find out the exact condition (if you do get a -1 for example).

By the same token, dozens of Java libraries only throw ABCException, with no other information... Might as well be -1.

Re: How can C Programs be so Reliable?

#15
Considering it's the first (alpha) release of the first significant C program he's written which is for a new (and therefore little used) programming language, and the page of Converge tools has no mention of testing tools, and the page itself has error messages on it, the claim that it's "not riddled with bugs" seems a touch, erm, cheerful.

Re: How can C Programs be so Reliable?

#16
post #5

People often write in higher level languages because they want lots of bad code fast. Almost all business applications are CRUD apps (create/retrieve/update/destroy) with some business logic, and they're generally written in C#. The app may crash when you click the wrong button, but the app is cheap to develop and the programmers are easily replaceable. Of course I'm generalizing, and a lot of C# programmers write gr…

I come from the systems research world. I've seen people choose C not for the reasons you mention, but just because it's what they know best. This is not always the best thing to do.

One instance is a colleague who needed to do data post-processing, and just did it in C because it was most familiar. A language like Perl or Python would have been a better choice, and saved him time in the long run, since string manipulation in C is tedious and error prone.

Re: How can C Programs be so Reliable?

#17
post #2

The author has a unique perspective since he - somehow - skipped learning C until now. He programmed assembly before, and from his other work he clearly is familiar with dynamic and more modern languages. Consequently, his perspective on C is that of somehow who is new to the language, yet also understands both the fundamentals of what his code will compile down to, and the higher level facilities that later language…

that was my experience when I went from ASM - to functional and then C when wrangling hardware. I am still surprised at how reliable C apps can be (linux kernel) - hats off to the genii involved in making things stable.

Re: How can C Programs be so Reliable?

#18

Earlier quoted context omitted.

What? What can a programmer do besides returning a "special" value such as -1 in a language like C with strict types and that doesn't support exceptions?

You don't need an exception mechanism to deal with this - good libraries will let you find out the exact condition (if you do get a -1 for example). By the same token, dozens of Java libraries only throw ABCException, with no other information... Might as well be -1.

But you can't ignore the ABCException, your app will crash. If you ignore the -1, then you're app will happily continue on doing something wrong.

Re: How can C Programs be so Reliable?

#19

Earlier quoted context omitted.

"As the author alludes to as well - in C you're made more aware of the error conditions you can handle and the ones you can't." You mean like when a function silently returns -1 to indicate failure and then you wonder why your program returned a wrong result (if you're lucky enough to even notice)? In the bigger part of most of my programs I want a big, flashy, loud, total failure by default if anything goes wrong.

if ((result = foo()) == -1) { fprintf(stderr, "!$*!$&^!$*!!!!\n"); exit(1); }

If exit() is too abrupt, you can always use setjmp() and longjmp() to set up a non-local jump to an error handler.

Re: How can C Programs be so Reliable?

#20

Earlier quoted context omitted.

if ((result = foo()) == -1) { fprintf(stderr, "!$*!$&^!$*!!!!\n"); exit(1); }

That won't scale. I'm sure you'll have lots of fun grepping through your source files looking for "!$ !$&^!$ !!!!" when it appears on your stderr. The point is that with an exception system you don't have to write stupid shit like that everywhere just to crash and burn correctly. Failing loudly is the only sensible behavior when an unexpected error occurs and there's absolutely no indication of what to do in such a c…

This is why assert exists.

tyger(~)% cat main.c

? #include

?

? void main( void )

? {

? assert( 0 == 1 ) ;

? }

? EOF

tyger(~)% gcc main.c

main.c: In function ‘main’:

main.c:4: warning: return type of ‘main’ is not ‘int’

tyger(~)% ./a.out

Assertion failed: (0 == 1), function main, file main.c, line 5.

Abort

tyger(~)%

Post reply on HN