Common Mistakes for New C Programmers¶
From a Quora question. I thought it'd make a nice list for interview questions.
- Not understanding static allocation vs. dynamic allocations
- Getting pointer arithmetic wrong
- Leaking memory
- Holding on to dangling pointers, and subsequent double frees thereof
- Off by one errors in string manipulation, particulary due to not being aware of the null terminator
- Not being careful about the subtle differences between memory routines vs. string routines
- Stack corruptions
- Heap corruptions
- Mismatched arguments and parameters for format strings
- Mistakes related to passing in a pointer instead of a pointer to a pointer to routines where you want a resulting pointer modified. Its easy for newcomers to get confused between the single and double indirection here
- Using uninitialized stack variables before an assignment
- Using = instead of == for equality comparisons
And more, which I'd probably classify as more advanced:
- Following the philosophy of try-it-and-see, where they write tests to infer rules about C -- this is impossible and damaging because of undefined behavior, which introduces nondeterminism;
- Not understanding undefined, unspecified, implementation-defined and locale-specific behaviors;
- Confusing pointers and arrays (they're completely different);
- Misunderstanding scope (people talk of global variables, even though C doesn't have them) and not knowing about linkage;
- Thinking about stacks and heaps instead of storage types (allocated, automatic, static and, new in C11, thread);