|
|
Subscribe / Log in / New account

Zeuthen: Writing a C library, part 1

Zeuthen: Writing a C library, part 1

Posted Jun 28, 2011 13:38 UTC (Tue) by cmccabe (guest, #60281)
In reply to: Zeuthen: Writing a C library, part 1 by wahern
Parent article: Zeuthen: Writing a C library, part 1

> Aborting in an OOM scenario isn't very good advice for a library

I agree. If you write your library to abort when a memory allocation fails, you are forcing that policy on the library user. He may not want it.

Even if you choose not to handle out-of-memory errors, calling abort() doesn't seem like the right thing to do. Just because your library can't get its job done doesn't mean that the developer necessarily wants to bring down the whole application. Maybe that job was something extremely minor and we just want to keep going.


to post comments

Zeuthen: Writing a C library, part 1

Posted Jun 28, 2011 15:48 UTC (Tue) by nix (subscriber, #2304) [Link] (1 responses)

I wonder if David was a Perl programmer before he was a C programmer? 'die on error' actually makes sense in Perl, because library users can trap it. But they can't trap abort(). You should abort() about as often as you BUG_ON() in the kernel: when you can't continue, your internal state is blown and can't be fixed, and the world is ending. And nothing you can predict (e.g. OOM) should lead you into such a state.

Zeuthen: Writing a C library, part 1

Posted Jun 28, 2011 16:18 UTC (Tue) by HelloWorld (guest, #56129) [Link]

> But they can't trap abort().
They can use longjump in the SIGABRT signal handler. But that probably doesn't make much of a difference, as a library that calls abort probably won't free allocated resources before it does, resulting in resource leaks.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds