Pointers (Pp Tminimizer)


Published on

Published in: Business, Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Pointers (Pp Tminimizer)

  1. 1. POINTERS IN C++
  2. 2. POINTERS : Introduction <ul><li>Every byte in computer memory has an address. </li></ul><ul><li>Addresses are numbers just like house numbers. </li></ul><ul><li>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 </li></ul><ul><li>The moment a variable is declared, memory is allocated in the RAM for its storage. int X=5; </li></ul><ul><li>A variable in the RAM can be accessed only if you know its address </li></ul>
  3. 3. POINTERS : Introduction <ul><li>A pointer in c++ gives the address of a variable in RAM </li></ul><ul><li>It gives direct access to the RAM . </li></ul><ul><li>A variable storing a memory address is called a Pointer </li></ul>
  4. 4. Uses of Pointers <ul><li>Pointers can be used for swapping variables without changing their physical address in memory. </li></ul><ul><li>A pointer helps in traversing an array. </li></ul><ul><li>Pointers are used to pass arguments to functions by reference. </li></ul><ul><li>Pointers are used to allocate memory dynamically and deallocate it. </li></ul><ul><li>Pointers are used for creation of data structures like linked lists. </li></ul>
  5. 5. DECLARATION AND INITIALIZATION OF POINTERS <ul><li>Pointer variables are declared like normal variables except for addition of unary * character as shown: </li></ul><ul><li>type * var_name; </li></ul><ul><li>e.g., int * iptr; </li></ul><ul><li>float * fptr; </li></ul><ul><li>char * cptr; </li></ul>
  6. 6. TYPES OF POINTERS <ul><li>Pointer to Integer </li></ul><ul><ul><li>int * ptr; </li></ul></ul><ul><li>Pointer to character </li></ul><ul><ul><li>char * z; </li></ul></ul>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. 7. <ul><li>Pointer to float </li></ul><ul><ul><li>consider the following program segment: </li></ul></ul><ul><ul><li>float marks=76.50; </li></ul></ul><ul><ul><li>float *p; </li></ul></ul>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. 8. <ul><li>Pointer to Void </li></ul><ul><li>This is general purpose pointer that can point to any data type. </li></ul><ul><li>It is defined as </li></ul><ul><li>void * ptr ; </li></ul><ul><li>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 . </li></ul>
  9. 9. Null Pointer <ul><li>A pointer variable must not remain un-initialized. </li></ul><ul><li>If you do not have any legal value, you can initialize it with NULL pointer value as shown below: </li></ul><ul><ul><li>float * ptr=NULL; </li></ul></ul>
  10. 10. <ul><li>Two special operators used with Pointers are : </li></ul><ul><li>Asterisk (*) and </li></ul><ul><li>Ampersand (&) </li></ul>
  11. 11. THE ‘ADDRESS OF’ OPERATOR (&) <ul><li>Consider following: int x=10; int * ptr; </li></ul><ul><li>0 1 2 3 4 . . . . . </li></ul><ul><li>In C++ & is address of operator. </li></ul><ul><li>If x is a variable with value 10 &x is its address i.e., 1000 as shown above. </li></ul><ul><li>It is a valid statement : ptr=&x; ptr will get 1000 i.e., the address of variable x. </li></ul>n 15 1000 1001 Address of x memory Two bytes required for storing one integer
  12. 12. VALUE AT OPERATOR ( * ) <ul><li>The unary operator (*) returns the value of the variable located at address following it. </li></ul><ul><li>e.g.,if ptr=&x; i.e., ptr contains the address of x then giving the command : </li></ul><ul><li>cout << * ptr; </li></ul><ul><li>will display the value held by the address 1000 which is 15 . </li></ul>NOTE : The operand of & operator is any variable but operand of * operator is essentially a pointer variable.
  13. 13. MEMORY ALLOCATION <ul><li>Pointers provide an essential tool for increasing the power of C++ by handling memory. </li></ul><ul><li>When C++ program is compiled, computer memory is divided into four regions as shown: </li></ul>STACK HEAP GLOBAL VAR . PROG. CODE Area used for function calls, addresses, arguments and local variables Area used for dynamic allocation of memory
  14. 14. Static And Dynamic Memory Allocation <ul><li>Static Memory Allocation : When memory is allocated at the time of compilation.e.g., </li></ul><ul><li>int a[50]; </li></ul><ul><li>Dynamic Memory Allocation : Memory is allocated during run time using new operator from the heap memory area. The delete operator de-allocates the memory. </li></ul>
  15. 15. Memory Management : Operators new and delete <ul><li>Arrays are used to set aside memory. The statement </li></ul><ul><li>int a[50]; </li></ul><ul><li>reserves memory for 50 integers. </li></ul><ul><li>The following approach doesn’t work: </li></ul><ul><li>cin>>size; </li></ul><ul><li>int a[size];// error array size must be constant </li></ul>
  16. 16. The new Operator <ul><li>new operator requests for memory allocation dynamically. </li></ul><ul><li>It returns a pointer to the beginning of the block of assigned memory. Its form is: </li></ul><ul><li>pointer = new type; </li></ul><ul><li>or </li></ul><ul><li>pointer = new type[Number of elements]; </li></ul>
  17. 17. For example: <ul><li>int *ptr; </li></ul><ul><li>ptr = new int[5]; </li></ul><ul><li>In this example, the operating system assigns space for 5 elements of type int in the HEAP and returns address to the variable ptr . </li></ul>n Heap memory ptr Memory space for 5 int
  18. 18. Free Storage Pool <ul><li>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. </li></ul>
  19. 19. The delete Operator <ul><li>Dynamic memory when not required should be freed so that it becomes available for future use. </li></ul><ul><li>Operator delete can be used for this purpose, whose form is : </li></ul><ul><li>delete ptr; </li></ul><ul><li> or </li></ul><ul><li> delete [ ] ptr; </li></ul>
  20. 20. <ul><li>If the program reserves many chunks of memory using new, eventually all the variable memory will be reserved and the system will crash. </li></ul><ul><li>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. </li></ul>The delete Operator (Contd.)
  21. 21. <ul><li>If a function allocates memory dynamically but forgets to release it later, it occupies some amount of memory every time it is executed. </li></ul><ul><li>It will leave abandoned memory blocks. This situation is known as a memory leak. </li></ul>Memory Leaks Reasons for Memory Leak <ul><li>Forgetting to use delete operator. </li></ul><ul><li>The delete operator is in an unreachable code area of the program. </li></ul><ul><li>Using new operator for allocating memory with a pointer that is already pointing to an allocated object. </li></ul>
  22. 22. Give the output of the following: Pointer prg1.cpp #include <iostream.h> #include <conio.h> void main() { clrscr(); int * ptr; int i=25; ptr=&i; cout<<i<<‘ ’; cout<<&i<<‘ ’; cout<<ptr<<‘ ’; cout<<* ptr <<‘ ’; cout<< <*<*(& i)<<‘ ’; }
  23. 23. Give the output of the following: Pointer prg2.cpp #include <iostream.h> #include <conio.h> void main() { clrscr(); char * cp; char ch=‘a’; cp=& ch; cout<<* ptr <<‘ ’; cout<< <*<*(& i)<<‘ ’; }
  24. 24. Give the output of the following: Pointer prg3.cpp #include <iostream.h> #include <conio.h> void main() { clrscr(); char *p1,*p2,*p3; int x=5; int y=7; p1=&x; p2=&y; cout << “ original values are : “;<<*p1<<*p2; p3=p1; p1=p2; p2=p3; cout << “ exchanged values are : “;<<*p1<<*p2; getch(); }
  25. 25. Arithmetic of Pointers <ul><li>Only addition and subtraction operations are possible with pointers. </li></ul><ul><li>But both behave differently with pointers, according to the size of data they point to. </li></ul>
  26. 26. So, if we write: Assume following pointers: int *iptr;// //assume it points to location 1000 char *cptr; //assume it points to location 2000 float *fptr; //assume it points to location 3000 iptr++; //iptr will be incremented by 2 cptr++; //cptr will be incremented by 1 fptr++; //fptr will be incremented by 4
  27. 27. n Heap memory iptr 1000 1002 int *iptr;// //assume it points to location 1000 n Heap memory iptr 1000 1002 iptr++; //iptr will be incremented by 2