4 ms·
Saying C is "very hard" is useful to dissuade the unprepared from writing buggy, vulnerable software, but for the experienced developer with modern tools, C is
by nitrogen 13y ago
Saying C is "very hard" is useful to dissuade the unprepared from writing buggy, vulnerable software, but for the experienced developer with modern tools, C is at worst "tedious". Some simple habits, like always writing your malloc() and free() calls at the same time in balanced pairs, can make C quite managable. Add valgrind, unit tests, and static analysis, and I'd have much more confidence in a good C program than an average program in a weakly typed language.
- shadowmint 13y agoI'm pretty sure the its very hard refers to string handling in C, and also fairly solid in my agreement that unless you have a very very good reason, that is one of the places most people are better off staying away from (especially once you start playing with C unicode constructs). (but your point is totally valid with regard to C in general~)
- plumeria 13y agoIs there a good string handling library for C?
- nitrogen 13y agoOther HNers have recommended bstring. I haven't tried it myself. http://bstring.sourceforge.net/ http://bstring.sourceforge.net/
- clarry 13y agoBString relies on undefined behavior for security: The reason is it is resiliant to length overflows is that bstring lengths are bounded above by INT_MAX, instead of ~(size_t)0. So length addition overflows cause a wrap around of the integer value making them negative causing balloc() to fail before an erroneous operation can occurr. I wouldn't touch this library.
- gizmo686 13y agoFor people not familiar with it, Valgrind will run your program in a VM and trace memory accesses. It detects when you read from unitialized/unallocated memory, don't free your memory, etc. Almost every time I have had a non-logic bug in C code, in had a corresponding warning in Valgrind.
- vezzy-fnord 13y agoSome simple habits, like always writing your malloc() and free() calls at the same time in balanced pairs, can make C quite managable. One can also use the Boehm GC if they feel it necessary.
- thinkpad20 13y agoI'm curious as to who uses a garbage collector in C. It seems like if you're using C, you probably are in a situation where you want as close control of that kind of stuff as possible (short of assembly).
- dllthomas 13y agoThe same thing can be said about malloc. It's a matter of degree, and different projects have (sometimes subtly) different requirements.
- dllthomas 13y agoExpanding - in particular, Boehm seems to only run collections on allocations. If your code is structured such that you don't allocate during periods when you need more precise control (already a good idea, if you are using malloc!) then this won't have an impact.
- vezzy-fnord 13y agoGCC and Mono use it internally, I believe. It's a very conservative garbage collector at the end of the day. It can be tweaked so as to be completely bare bones, and the performance impact is very benign. Rather, memory consumption is its weakness.
- dllthomas 13y ago"malloc() and free() calls at the same time in balanced pairs" There is no stable correspondence between number of malloc calls and number of free calls. I might allocate things in 10 places that get freed in one, or vice-versa. A simple example would be "parse a packet and send the built packet (through a message queue) to handling code."
- nitrogen 13y agoWhile it's true that there are times when you can't achieve it, I intend to suggest limiting oneself to design patterns that facilitate simpler memory management, and implementing both halves of the memory management equation at the same time. Naturally there's a tradeoff; if redesigning your code to allow one malloc() to one free() would introduce more bugs in logic than it would solve in memory issues, then it's not worth it.