In C/C++ programming, pointer is often a pain if not handled correctly as there are lots of issues being faced related to pointer like invalid memory,uninitialize pointers.In this post we will be seeing some of the common causes of pointer issues.

Uninitialized pointers : This is one of the most common bugs for the starters where one forgets to initialize the pointer and try to reference the value of pointer which does not point to a valid address.For e.g:

int *ptr;

*ptr=1;

Here pointer p is initialized and points to random location in memory when you declare it.This random location can be anywhere in system stack or global variables or operating system.When one say *ptr=1, the program will simply try to write to 1 on the random location ptr points. This can result into corruption of data in another part of your program or segmentation fault and it will be very hard to track it down. So one should make sure one should initialize pointers to valid address before dereferencing them.

Invalid Pointer Reference : An Invalid pointer reference occurs when a pointer’s value is referenced even though the pointer does not point to a valid block. Let’s say we have ptr=ref where ref is uninitialized. The pointer ptr will then become initialized as well and any reference to *ptr is an invalid pointer reference. One can avoid this problem by making sure that all the pointer point to some valid location otherwise this can cause the program to crash.

Zero Pointer Reference : A zero pointer reference occurs whenever a pointer pointing to zero is referencing to a block. For example, if ptr is a pointer to an integer, the below code is invalid:

ptr=0;

*ptr = 1;

Since there is no block pointed to by ptr. Hence, trying to read or write anything from or to that block is an invalid zero pointer reference and dereferencing such a pointer is invalid.

Freed Pointer Reference :Free() function is used in C programming to release the dynamically allocated memory.After calling free() function, pointer still point at the same allocated memory address. So, you might succeed but it is illegal and behavior of accessing freed pointer (memory location) is undefined. The freed pointer is also called the dangling pointer. check the below example

#include <stdio.h> #include <stdlib.h> int main() { int *ptr = NULL; // allocating memory for 3 int ptr = malloc(sizeof(int) * 3); // Unable to allocate memory if (ptr == NULL) return 0; // Free the allocated memory free(ptr); // ptr is dangling pointer // Behaviour of this is undefined *ptr = 10; return 0; }

To avoid this kind of this issue one should always NULL to freed pointer.

Valid Return Value of Memory Allocation : While using memory allocation function like malloc/calloc make sure to check the return value from the function .If it is NULL then malloc failed to get memory else it is success.We should add this check while allocating memory as this ensure in preventing segmentation fault. See example below:

int *ptr = malloc(sizeof(int));



//Bad usage

*ptr= 23;



//Good usage

if(ptr == NULL)

return;

*ptr= 23

Dynamically Allocated Memory is not Freed : While allocating memory one must ensure to deallocate the memory too otherwise it will result in memory leak which can make the system ran out of memory if the same piece of code being called continuously. See below example

#include <stdio.h> #include <stdlib.h> int main () { // Allocate memory for 4 integer int * ptr = malloc(sizeof(int) * 20); //----Bad Case ------// // Not freeing the allocated memory return 0; //----Good Case-----// free(ptr); return 0; }

Double Freeing the Memory : Since we already seen that we should free the allocated memory but one should ensure that he/she should not free the same memory twice otherwise the behavior is undefined.To avoid these kind of problem,assign NULL to the deallocated pointer because free() function does not perform anything with null pointer.

#include <stdio.h> #include <stdlib.h> int main() { //initialize pointer with NULL int *ptr = NULL; // Allocate integer of size 10. ptr = malloc(sizeof(int) * 5); // Check if memory allocated successfully if (!ptr) { printf("Unable to allocate memory"); return; } // free dynamically allocated memory free(ptr); // Assign NULL to deallocated memory ptr = NULL; // Free dynamically allocated memory twice free(ptr); return 0; }

References: https://codeforwin.org/2018/10/five-common-pointer-mistakes-c-programming.html

If you have any feedback/suggestions please reply by adding a comment