LWN.net Logo

Why C?

Why C?

Posted Nov 11, 2004 19:29 UTC (Thu) by ncm (subscriber, #165)
In reply to: Sendmail X by minichaz
Parent article: Sendmail X

Good question. It's easy to write code that is proof against buffer overruns in C++, with no cost to performance. Furthermore, the standard library probably has the data structures you need, implemented with much greater care than you could afford to do yourself.

It's hard to imagine why anything other than a kernel module is written in raw C any more. It's easy, though, to see why someone wouldn't want a long-running daemon with a garbage collector.


(Log in to post comments)

Why not Garbage Collection?

Posted Nov 11, 2004 19:50 UTC (Thu) by shapr (subscriber, #9077) [Link]

There are many incremental garbage collection algorithms, why would they be unsuited for a long-running daemon?

I've used quite a few long-running processes (like webservers) that use generational garbage collection (Haskell, GHC). They work fine.
Java uses GC, and it has some success with long-running daemons also.
Python uses reference-counting, which I think counts as GC, and it also has some success with long-running daemons.

Does GC tend to leak memory or something? What disadvantages do you know about that I might not?

Why C?

Posted Nov 12, 2004 15:44 UTC (Fri) by dps (subscriber, #5725) [Link]

It is claimed that

>It's easy to write code that is proof against buffer overruns in C++, with
>no cost to performance. Furthermore, the standard library probably has the
>data structures you need, implemented with much greater care than you >could afford to do yourself.

This is *completely* wrong in my experience. I can not write C++ code with the same efficiency as C code. In particular C++ std::string wastes cycles on copies or reference counts. In my expiernece C++ requires things like reference counts if you want to avoid nasty suprises (something goes out of scope somewhere and disappears prematurely).

You can get smaller and faster code by making sure every persistent object has an owner, who is responsible for freeing it or passing it on to someone else. This is not much more complex either, unless your design is broken.

I avoid the C++ algorithms and data structures because how they are implemented is too vague for my taste---I choose algorithms and data structures based on the data and how my program uses it. It is also The implementation really does matter, despite what the OO people claim.

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