I’ve observed that newcomers to C often getting messed up, even C experts come across problems that require days of debugging to fix. I have seen so many people having difficulty in differentiating between char *book and char book. People get’s errors due to using one instead of the other.
I feel rather calling it as C pit fall, should have called common mistakes made by new comers or some times even experienced programers. This artical is publish to make aware of these common pit falls while programing in C.
As pointed out by others, it is easy to get confused between static and dynamic allocation and when to use which.
Following is the list of common pit falls in C
- 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
- 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 and not knowing about linkage;
- Thinking about stacks and heaps instead of storage types (allocated, automatic, static and, new in C11, thread);
- Getting used to the memory management model is a pain for a lot of people.
- Dereferencing object pointers rather than using properties
- Some people also try to continue writing regular C functions instead of Objective C methods
- A lot of general heartache seems to stem from trying to fight against NSFoundation/Cocoa to blaze your own trail. Things are much, much easier if you take the time to figure out the “Apple Way” is to do tasks
- Over-reliance on protocols and delegates when NSNotificationCenter is often much cleaner for doing what you want
- Memory Management. (it’s worth saying twice)
I request all viewers please comment on this topic and discuss the difficulties you found with C, I will add there points to this list…hope one day we can have a list to make C programing without frustration….Thank you!
I would like to request viewers, please correct us if you find any point mentioned above is wrong or incorrect.