As pointers are little different from any other variable, and still considered as normal variable, it is obvious that we forget that it is a pointer. Hence we may end up in some common mistakes while creating pointer variables.
Not assigning Memory
Whenever we create a pointer variable, it is very much essential to assign memory to them. Since it is a pointer, compiler will expect some memory address while compiling it. Sometimes compiler will assign some memory from the system to it, which are not intended for program or pointers. Hence using such memory for the pointer will crash the system. Sometimes pointer will not be assigned any memory and will be hanging in the system. This is also not expected, as it will create wrong interpretation to the compiler. Hence we need to assign some free memory to it or at least assign NULL to them so that compiler understands that it is not hanging around the system. In addition assigning memory to the pointers will make those memory blocks set for these pointers and will not allow any other pointer / program to use them.
If we do not assign any memory to the pointer variable, compiler will not identify while compiling. Hence we will see error free code. At the run time all the disaster will happen!
Suppose we have declared and assigned value as below: Compiler will not show any error in below case as some random memory address will be allocated to intX while declaring it.
int *intPtrX; // No memory is allocated to the pointer variable * intPtrX = 30; //still value is assigned to it, which may change the value at some illegal location! </code>Hence it is very much necessary to make the pointer to point to correct memory address as below:</pre> <pre><code>int *intPtrX; // No memory is allocated to the pointer variable intPtrX = &intY; // Memory address of intY is allocated to pointer, hence it will point to correct address in the system *intPtrX = 30; // it will change the value on intY now rather than changing some unknown values
int *intPtrX = NULL;
int *intPtrX = malloc(sizeof(int));
Like we assign the memory to the pointer, it is also important to free them, so that those memories can be used by other pointers / programs. This makes the perfect programming.
Illegal Indirection while allocating and referencing pointers
While creating, assigning and using the pointers, we need to very careful and need to understand which values of pointers are being used and is correctly used. That means suppose we are allocating memory using malloc function. This malloc function returns the pointer to the memory block which is assigned to the pointer if it gets memory; else returns NULL. Suppose we have allocated memory like below:
*intPtrX = malloc(sizeof(int));
Do you think above syntax is correct? No, it is not correct. Why? Since malloc is returning the address if it is success, using *intPtrX will change the value pointed by intPtrX to the address value rather than making it to point to the address returned by malloc.
Hence correct way of allocating memory is as below. Now intPtrX is pointing its own value which can be modified.
intPtrX = malloc(sizeof(int));
We can notice that in the above example of ‘Not assigning memory’, we have allocated memory using ‘*’. But here memory is allocated while declaring a pointer itself. Hence we have used ‘*’. Here, compiler will first declare a pointer variable *intPtrX and then it will allocate memory to it as shown above. Hence compiler will consider below line of code in 2 parts: int *intPtrX and intPtrx = malloc(sizeof(int)).
int *intPtrX = malloc(sizeof(int));
Another illegal assignment to pointer variable is observed while making it to point to some variable. Observe below code snippet which is wrongly assigned to point to variables. Utmost care should be taken to assign the correct value at both left and right hand side (lvalue and rvalue) of assignment operator.
int intX *intPtrX; intPtrX = intX; // intPtrX is address whereas, intX is not an address. * intPtrX =& intX; // & intX is address whereas, * intPtrX is not an address.
Hence the correct way of assigning the address and value to the pointer variable is as below:
int intX *intPtrX; intPtrX =& intX; //Both &intX and intPtrX are address * intPtrX =intX; //Both intX and *intPtrX are values