Things Not To Do with Pointers in C Programming

Do Not Dereference Null Pointers



Dereferencing a null pointer leads to undefined behavior, typically resulting in a segmentation fault or crash.

Do Not Use Dangling Pointers



A dangling pointer points to memory that has been deallocated or is no longer valid. Using such a pointer can corrupt data or crash the program.

Do Not Perform Unchecked Pointer Arithmetic



Pointer arithmetic that exceeds the bounds of the array it points to can lead to undefined behavior.

Do Not Assume Pointers To Different Types Are Interchangeable



Using pointers of one type to manipulate data intended for another type can lead to misinterpretation of the data layout, alignment issues, or crashes.

Do Not Forget To Check Return Value From Malloc



If malloc fails, it returns NULL. Using this pointer without checking can lead to crashes.

Do Not Write Beyond The Allocated Memory



Writing outside the bounds of allocated memory can corrupt data, cause crashes, or lead to security vulnerabilities.

Do Not Mix Dynamic And Static Memory Management



Using a combination of malloc/free with array declarations or stack memory can confuse ownership and lifetime rules, leading to errors.

Do Not Reuse Freed Pointers Without Reinitialization



After free(), a pointer becomes a dangling pointer. Reusing it without proper reinitialization can lead to undefined behavior.