Spinning off from from Developing Max Externals (Tips and Tricks), and Structured Programming, here’s a (selfishly motivated) thread to collect C programming tips and tricks. I’m puzzling my way through C (coming by way of many years programming with modern conveniences like class inheritance, first class functions, namespaces, etc.) and am only just finding my legs. Maybe some of my bumblings will be useful to others? Better still, maybe some of you with real C chops (err, @tehn, @zebra, @sam) will chime in!
Anyway, I’ll start. Nothing authoritative. Just observations from the road…
You need to. Really.
A few pointers (hah):
Malloc Scribble FTW. To help keep you honest (and sane), consider using whatever your platform offers in the way of tweaking memory allocation parameters in support of debugging. It’s very easy to convince yourself (especially in unit tests) that you’ve properly initialized your structs when you haven’t. To guard against this, look at options such as
M_PERTURBfor malloc (in
mallopt.hin linux) that cause allocated bytes to be initialized to non-zero values. XCode has memory management diagnostic settings for schemes that are vey handy for this too (notably “Enable Malloc Scribble”: Product>Scheme>Edit Scheme… => Run/Diagnostics).
- Understand pass by value semantics. Dust off K&R. It’s worth it.
- Know that there is a stack and a heap. Acceptance is the first step. Lots and lots has been written about the distinction and why you should care. Apparently it’s even a popular interview question!