Pointers (Pp Tminimizer)

  • View

  • Download

Embed Size (px)



Text of Pointers (Pp Tminimizer)

  • 1. POINTERS IN C++
  • 2. POINTERS : Introduction
    • Every byte in computer memory has an address.
    • Addresses are numbers just like house numbers.
    • Addresses start from 0 and go up as 1, 2, 3.e.g. in 640 KB memory addresses will be 0 to 655359 I.e., (640 X 1024)-1
    • The moment a variable is declared, memory is allocated in the RAM for its storage. int X=5;
    • A variable in the RAM can be accessed only if you know its address
  • 3. POINTERS : Introduction
    • A pointer in c++ gives the address of a variable in RAM
    • It gives direct access to the RAM .
    • A variable storing a memory address is called a Pointer
  • 4. Uses of Pointers
    • Pointers can be used for swapping variables without changing their physical address in memory.
    • A pointer helps in traversing an array.
    • Pointers are used to pass arguments to functions by reference.
    • Pointers are used to allocate memory dynamically and deallocate it.
    • Pointers are used for creation of data structures like linked lists.
    • Pointer variables are declared like normal variables except for addition of unary * character as shown:
    • type * var_name;
    • e.g., int * iptr;
    • float * fptr;
    • char * cptr;
    • Pointer to Integer
      • int * ptr;
    • Pointer to character
      • char * z;
    Type of variable pointed to by ptr Reference operator Name of the pointer Type of variable pointed to by z Name of the pointer Reference operator
  • 7.
    • Pointer to float
      • consider the following program segment:
      • float marks=76.50;
      • float *p;
    Currently p is not pointing to any variable 76.50 marks A variable of type float If following statement is added in the above program segment: p = & marks; The variable p will point to marks of the type float as shown below: 76.50 marks p A variable of type float Now pointer p is pointing to marks p
  • 8.
    • Pointer to Void
    • This is general purpose pointer that can point to any data type.
    • It is defined as
    • void * ptr ;
    • Limitation : The data pointed to cannot be referenced directly. (We cannot use * operator on them.), since their length is undetermined. We will have to resort to type casting .
  • 9. Null Pointer
    • A pointer variable must not remain un-initialized.
    • If you do not have any legal value, you can initialize it with NULL pointer value as shown below:
      • float * ptr=NULL;
  • 10.
    • Two special operators used with Pointers are :
    • Asterisk (*) and
    • Ampersand (&)
    • Consider following: int x=10; int * ptr;
    • 0 1 2 3 4 . . . . .
    • In C++ & is address of operator.
    • If x is a variable with value 10 &x is its address i.e., 1000 as shown above.
    • It is a valid statement : ptr=&x; ptr will get 1000 i.e., the address of variable x.
    n 15 1000 1001 Address of x memory Two bytes required for storing one integer
  • 12. VALUE AT OPERATOR ( * )
    • The unary operator (*) returns the value of the variable located at address following it.
    • e.g.,if ptr=&x; i.e., ptr contains the address of x then giving the command :
    • cout >size;
    • int a[size];// error array size must be constant
  • 16. The new Operator
    • new operator requests for memory allocation dynamically.
    • It returns a pointer to the beginning of the block of assigned memory. Its form is:
    • pointer = new type;
    • or
    • pointer = new type[Number of elements];
  • 17. For example:
    • int *ptr;
    • ptr = new int[5];
    • In this example, the operating system assigns space for 5 elements of type int in the HEAP and returns address to the variable ptr .
    n Heap memory ptr Memory space for 5 int
  • 18. Free Storage Pool
    • Every program is given some unallocated heap memory that is used by the program for dynamic memory allocation during execution. This is known as free storage pool.
  • 19. The delete Operator
    • Dynamic memory when not required should be freed so that it becomes available for future use.
    • Operator delete can be used for this purpose, whose form is :
    • delete ptr;
    • or
    • delete [ ] ptr;
  • 20.
    • If the program reserves many chunks of memory using new, eventually all the variable memory will be reserved and the system will crash.
    • To ensure safe and efficient use of memory, the use of new operator is followed by corresponding delete operator that returns memory to operating system.
    The delete Operator (Contd.)
  • 21.
    • If a function allocates memory dynamically but forgets to release it later, it occupies some amount of memory every time it is executed.
    • It will leave abandoned memory blocks. This situation is known as a memory leak.
    Memory Leaks Reasons for Memory Leak
    • Forgetting to use delete operator.
    • The delete operator is in an unreachable code area of the program.
    • Using new operator for allocating memory with a pointer that is already pointing to an allocated object.
  • 22. Give the output of the following: Pointer prg1.cpp #include #include void main() { clrscr(); int * ptr; int i=25; ptr=&i; cout