Pointer & Dynamic memory

  • Pointer: A pointer is simply the address of a memory location and provides an indirect way of accessing data in memory. A pointer variable is defined to 'point to' data of a specific type. For example
    int *ptr1;//pointer to an int
    char *ptr2;//pointer to char
    

  • The value of a pointer variable is the address to which it points. For example, given the definitions int num; we can write: ptr1 = #
  • The symbol & is the address operator;
  • it takes a variable as argument and returns the memory address of that variable. The effect of the above assignment is that the address of num is assigned to ptr1. Therefore, we say that ptr1 points to num.
  • In general, the type of a pointer must match the type of the data it is set to point to.
  • A pointer of type void*, however, will match any type. This is useful for defining pointers which may point to data of different types, or whose type is originally unknown.
  • A pointer may be cast (type converted) to another type. For example, ptr2 = (char*) ptr1; converts ptr1 to char pointer before assigning it to ptr2.
  • Regardless of its type, a pointer may be assigned the value 0 (called the null pointer). The null pointer is used for initializing pointers, and for marking the end of pointer-based data structures (e.g., linked lists).
    Example for pointers

    Dynamic memory

  • In addition to the program stack (which is used for storing global variables and stack frames for function calls), another memory area, called the heap, is provided.
  • The heap is used for dynamically allocating memory blocks during program execution.
  • As a result, it is also called dynamic memory. Similarly, the program stack is also called static memory.
  • Two operators are used for allocating and deallocating memory blocks on the heap. The new operator takes a type as argument and allocated a memory block for an object of that type. It returns a pointer to the block.
    For example, 
    int *ptr = new int;
    char *str = new char[10];
    
  • allocate, respectively, a block for storing a single integer and a block large enough for storing an array of 10 characters.
  • Memory allocated from the heap does not obey the same scope rules as normal variables.
    For example, in
    void Foo(void)
    {
    char *str = new char[10];
    //..
    }
    
  • when Foo returns, the local variable str is destroyed, but the memory block pointed to by str is not. The latter remains allocated until explicitly released by the programmer.
  • The delete operator is used for releasing memory blocks allocated by new. It takes a pointer as argument and releases the memory block to which it points.
    For example:
    delete ptr; 	// delete an object
    delete [ ] str; 	// delete an array of objects
    
  • Note that when the block to be deleted is an array, an additional [ ] should be included to indicate this.
    1. #include < string.h >
    2. char* CopyOf (const char *str)
    3. {
    4. char *copy = new char[strlen(str) + 1];
    5. strcpy(copy, str);
    6. return copy;
    7. }
    
  • Annotation
    1-This is the standard string header file which declares a variety of functions for manipulating strings.
    4 -The strlen function (declared in string.h) counts the characters in its string argument up to (but excluding) the final null character. Because the null character is not included in the count, we add 1 to the total and allocate an array of characters of that size.
    5 - The strcpy function (declared in string.h) copies its second argument to its first, character by character, including the final null character.

  • New AND Delete Operators in Classes

  • The new operator is used in the constructor so that the memory is allocated when the object is created.
  • The delete operator is usually used in the destructor to return the memory to the operating system when the object is destroyed.
    Example

    An Array of Pointers to Objects

  • A common programming construction is an array of pointers to objects.
  • This arrangement is more flexible than placing the objects themselves in an array.
    Example
  • The main ( ) function defines an array, persprt, of 100 pointers to person. In a do loop it then asks the user to enter a name.
  • With this name it creates a person object using new, and stores a pointer to this object in the array persptr.

    Pointers to Derived Types

  • In general, a pointer of one type cannot point to an object of a different type.
  • However, there is an important exception to this rule that relates only to derived classes.
  • To begin, assume two classes called B and D. Further, assume that D is derived from the base class B.
  • In this situation, a pointer of type B may also point to an object of type D.
  • More generally, a base class pointer can also be used as a pointer to an object of any class derived from that base.
  • Although a base class pointer can be used to point to a derived object, the opposite is not true. A pointer of type D may not point to an object of type B
  • Further, although you can use a base pointer to point to a derived object, you can access only the members of the derived type that were imported from the base.
  • That is, you won't be able to access any members added by the derived class. (You can cast a base pointer into a derived pointer and gain full access to the entire derived class, however.)
    Example


    Multiple Inheritance<< Previous

    Next >> This Pointer

    Our aim is to provide information to the knowledge seekers.


    comments powered by Disqus


  • Footer1