1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Practical Guide to using of Pointers

Discussion in 'C' started by Mridula, Feb 27, 2010.

  1. Mridula

    Mridula New Member


    This article talks about some general tips while using pointers in an Application.


    While working with different kinds of errors, generally detecting an error is easier than fixing it. When it comes to pointer, the error is usually the result of a pointer pointing somwehere else it should not. And using such pointer is nothing but working on memory which is corrupted.

    While working with pointers, we need to keep two points in mind
    1. Either avoid introducing pointer-error in the first place.
    2. Or fix the pointer-error as soon as we code them as possible.

    Here are some general tips on pointer while using them in our program.

    1. Isolate pointer operations in Routines or Classes.

    Suppose if we are using linked list in several places of an application, then instead of using the list directly in each place it's used, it is better to have access routines like CurrentNode(), PrevNode(), NextNode(), DeleteNode(), InsterNode() etc & use these access routines everywhere the list is used. This in turn helps in re-usability.

    2. Avoid acquiring pointers to non-heap memory.

    For Example:

    The code

    void function() 
        int a;
        int *ptr;
        *ptr = &a;
        // do something here
    Here, you may forget that ptr is pointing to an object whose life is over when the function returns, which will lead to all kinds of dangerous errors.

    3. Declare and Define pointers at the same time.

    First of all, assiging a Variable it's initial value close to where it is declared is one of the coding guideline we usually need to follow. And this coding guideline is more valuable when it comes to pointer variables.

    For Example:

    Bad Practice:

    Library *libPtr;

    //Lots of other code here

    libPtr = new Library;

    Here there are chances that libPtr being used in between the code (between it's declaration and it's initialization) before even it gets allocated and initialised properly.

    Good Practice:

    Library *libPtr;
    libPtr = new Library;

    4. Delete Pointers at the same scoping level as they were allocated.

    Always keep Allocation and De-allocation of pointers symmetric.

    For Example,

    Bad Practice:

    A Routine allocating memory to a pointer and expecting it's client code to de-allocate that pointer. This creates in-consistency which in-turn may lead to errors.

    Good Practice:

    if we allocate and initialize a pointer in a class's constructor, it should to be de-allocated/deleted in the class's destructor.

    5. Always test pointers before using them.

    Before using pointers in our application, make sure the memory location it points to is valid.

    For Example:

    If we expect memory location for our application to begin with start_data and end with end_data, in such case, check the pointer whether it is pointing in between start_data and end_data location. We can do this by having access routines rather than directly manipulating them.

    6. Also test the pointers for NULLness before using them in application.

    Bad Practice:

    void function(Library *libPtr) {

    Good Practice:

    void someFunction(Library *libPtr) {
    if ( !libPtr ) {
    // throw an exception / assert it with proper message

    We can write a generalised Assert for the application and use it everywhere to test the pointers before using them, rather than directly checking everytime.

    7. Check the Value of the variable pointed by pointer before using it.

    Sometimes it is good to check the value of the pointer it points to before working on it.

    For Example:

    Suppose you expect an Interger pointer to point to some integer value that takes value like 0-1000 range. Then after de-referencing pointer and before using the value, check whether the value falls in the correct range or has some different value, before working on it.

    As said in last tip, this also can be done using some access routines.

    8. Simplify complicated pointer expression.

    Complex pointer expressions are hard to read.

    For Example:

    Bad Practice:

    for(index=0; index<LIST_SIZE; ++index)
    netRate[index] = baseRate[index] * rates->disCount->factor->net;

    Good Practice:

    discountValue = rates->disCount->factor->net;

    for(index=0; index<LIST_SIZE; ++index)
    netRate[index] = baseRate[index] * discountValue;

    This not only helps for better redability, it adds even to improve the performance of code, by giving the benefit of not doing the discountValue calculation inside loop for every iteration.

    9. Initialize pointer to NULL.

    If you are not immediately initializing a pointer to some valid value, always initialize it to NULL.

    Good Practice:

    Employee *empPtr;
    empPtr = NULL;

    This protects our code from leaving a pointer un-initialized and later doing as below:

    if (empPtr)
    delete empPtr;

    10. Shred your garbage.

    Pointer Errors are hard to debug because the location in the memory, at which the pointer points to, becomes invalid, is non-deterministic. Sometimes the memory contents look valid, even after the pointer is freed.

    So, we can force errors related to using deallocated pointers to be more consistent by overwriting freed meomry blocks with junk-data before deleting the pointer.

    Good Practice:

    memset(empPtr, GARBAGE_DATA, memBlockSizeOf(empPtr));
    delete (empPtr);

    11. Set pointer to NULL after deleting it.

    Also, common error with pointer is "dangling pointer", use of pointer that has been deleted or freed.

    Good Practice:

    delete (myPtr_mp);
    myPtr_mp = NULL;

    By setting pointer to NULL after freeing it, we don't change the fact of reading data pointed by the dangling pointer, but we do ensure that not writing data to such pointer, as writing to such pointer will yeild an error or result in segmentation fault.

    12. Check for bad pointers before deleting it.

    Good Practice:

    Assert(empPtr!=NULL, "Attempting to delete a NULL pointer");
    empPtr = NULL;

    13. Use of new[] and delete[] properly.

    Inconsistent usage of new[] and delete[] lead to memory leaks. The most common mistake is using new[] to allocate and using just delete to deallocate. The effect is that we construct an array of objects, then destruct only the first element of it.

    14. Draw a Picture.

    It usually helps to draw a picture while working on linked list.

    For Example:
    If we are adding a newNode to the existing list. Then we can draw a picture something like below. This would help coding easier.


    Hope it helps!!
  2. loyo

    loyo New Member

    Thank you so much for nice information.
    I would pay attention to my code.
  3. shabbir

    shabbir Administrator Staff Member

  4. seangtz

    seangtz New Member

    Thanks for sharing I will try it out
  5. fashionbop

    fashionbop New Member

    Although it is very easy,but we forgot it sometimes.
    Thank you for sharing,there are good suggestions.
  6. shabbir

    shabbir Administrator Staff Member

  7. Tobiasgar

    Tobiasgar New Member

    Thanks for the clear explanation )) will follow all the steps:baby:

Share This Page