Page 70 - DCAP407_DATA_STRUCTURE
P. 70

Unit 4: Pointers



               The preceding example is valid because both a and b are pointers. However, the next example  is not
               valid because 50 is a numeric constant and not a pointer type.


                              int *a;
                              If (a > 50)
                              {
                              }

               Table 4.2 shows some of the valid and invalid operations that can be performed on the pointers.

                                        Table 4.2: Valid and Invalid Pointer Operations


                      Arithmetic operators        Invalid operation          Valid operation
                  Addition                   m1 + m2                   m1+2
                  Subtraction                m1-m2                     m1-2
                  Multiplication             m1*m2                     *m1×*m2



                           Equality operator (==) and inequality operator (!=) can be applied only if one of the
                           operand is a null pointer ( NULL or ‘\0’.)


               4.3   Dangling Pointers

               A dangling or wild pointer is a pointer which does not point to a valid memory location. This means
               that a running process which has certain restrictions on accessing the memory location does  not fall
               under the address space. A pointer if not handled properly produces serious bugs or a bad program.
               Dangling pointers can be created in many ways.

                                     {
                                            char *p = NULL;
                                            {
                                                    char a;
                                                    p = &a;
                                            }
                                     //memory location which a was occupying is released
                                     // p is now a dangling pointer
                                     }

               Dangling pointer can be avoided by making p as a null pointer after exiting from the inner block. A
               dangling pointer in a program always points to a memory location outside the process space. Here, the
               location pointed by the dangling pointer may or may not contain a valid object. If it is modified, then
               the valid object’s value can change unexpectedly distorting the performance of the process which owns
               the object. This is called memory corruption. This leads to an erratic behavior of the system which will
               finally crash the system.

               Dangling pointers can be avoided by initializing them to “NULL” during their declaration or when they
               are not used in the program.
               A common programming error while creating a dangling pointer is returning the address of the local
               variable.









                                        LOVELY PROFESSIONAL UNIVERSITY                           63
   65   66   67   68   69   70   71   72   73   74   75