Pointers

592 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
592
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
35
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Pointers

    1. 1. Pointers in C++presentation by Frankie Onuonga twitter: FOnuonga
    2. 2. definition• this is one of the most powerful ways to manipulate low level computer memory• by definition a pointer is therefore a variable that holds a memory address
    3. 3. tasks• what pointers are• how to declare and use pointers• what the free store is and how to manipulate memory
    4. 4. memory basics• computer memory is divided into sequential numbere memory locations• each declared variable is stored into a unique memory location known by its address• different computers have different ways of numbering the memory locations
    5. 5. • as a user you do not require to know this scheme but if one is interested you can use the & operand to retrieve this.• therefore is in a program you define : • typedef unsigned long int USHORT; • USHORT location; • std::cout<<“location is at: “<<&location<<endl;
    6. 6. • when you declare a variable the computer takes care of size factor in terms of how much memory is required to store it and assignes an address to the variable location.
    7. 7. relation between variable and pointer• every variable has an address• without knowing the address one can store the variable address in a pointer• to declare a pointer which is also a variable we use the form : • int *page=0;
    8. 8. • the page variable is a pointer to an int. it is a special kind because it has the * sign infront of it.• it is important to note we have set the value to zero also known as a null pointer• all pointers when initialized should be assigned a value• is it is not assigned anything it is a wild pointer which can be very dangerous
    9. 9. • Now that a pointer has been declared then we can go ahead and assign it a value. this is usually done by the use of: • page=&location
    10. 10. getting values from variables• using the pointer one can determine the value of the variable• indirection: accessing the value from the variable by use of a pointer• it also means accessing the value at the address held by the pointer.
    11. 11. dereferencing with the indirection operator• the indirection pointer(*) is also called the dereference operator• when a pointer is dereferenced then the value at the address stored by the pointer is retrieved
    12. 12. Is the result the same here• do these two pieces of code give the same result
    13. 13. pointer, address and variables• it is important to distinguish between a pointer , the value held by the variable and the pointer.• it should be noted the address is what is held by the pointer.• the variable is usually a memory location declared to hold a variable.
    14. 14. manipulation by use of pointers• in addition by use of the indirection method one can go ahead and manipulate the data the pointer points to• this is done by means of *pointer=value wanted
    15. 15. why would you use pointers• pointers are realistically used in three ways(the first two are the main ones): • manageing data in the free store • accessing class member data and variables • passing variables by reference to functions
    16. 16. recall• the main five areas of memory are mentioned in any memory class: • global namespace • the free store • registers • code space • the stack
    17. 17. allocating space with the new keyword• this is a way to allocat memory on the free store.• the keyword is used along with the type of object you would like to allocate• while declaring this it should also be associated with a pointer and thus • unsigned short int * pPointer = new unsigned short int;
    18. 18. the delete keyword• this is done to put memory back.• that is to free the memory.• it is critical to remember that memory allocated with new is not freed automatically• important to note here is memory leak• this is done by use of delete
    19. 19. • essentially what happends is that the memory alocated is retunred back to the free store.• the pointer can still be used
    20. 20. creating objects on the free store• a pointer can also point to any data type just as it points to an integer• the creating rules are simsilar to the one of creating one that is an integer.• all rules apply in that if it has been made it has to be cleaned by hard core code.• thus one uses ( and is not limited to) the constructor and deconstructer.
    21. 21. using const pointers• there are generally three ways to use the const keyword: • cont int * pOne; • it is a pointer to a constant integer. the value being pointed to can not be changed • int *const pTwo; • pTwo is a constant pointer to an integer. The integer can be changed, but pTwo can’t point to anything else
    22. 22. • const int * const pThree • pThree is a constant pointer to a constant integer. The value that is pointed to can’t be changed, and pThree can’t be changed to point to anything else.• The trick to keeping this straight is to look to the right of the keyword const to find out what is being declared constant

    ×