Pointers in C++presentation by Frankie Onuonga twitter: FOnuonga
deﬁnition• this is one of the most powerful ways to manipulate low level computer memory• by deﬁnition a pointer is therefore a variable that holds a memory address
tasks• what pointers are• how to declare and use pointers• what the free store is and how to manipulate memory
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
• 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 deﬁne : • typedef unsigned long int USHORT; • USHORT location; • std::cout<<“location is at: “<<&location<<endl;
• 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.
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;
• 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
• 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
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.
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
Is the result the same here• do these two pieces of code give the same result
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.
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
why would you use pointers• pointers are realistically used in three ways(the ﬁrst two are the main ones): • manageing data in the free store • accessing class member data and variables • passing variables by reference to functions
recall• the main ﬁve areas of memory are mentioned in any memory class: • global namespace • the free store • registers • code space • the stack
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;
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
• essentially what happends is that the memory alocated is retunred back to the free store.• the pointer can still be used
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.
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
• 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 ﬁnd out what is being declared constant