Pointers A pointer is a memory address of an object of a specified type, or it is a variable which keeps such an address. Pointer properties: P (pointer) 12316 12316 (address) Typed object A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to. Teresa Leyk Slide 1 C++ Review Pointers
Pointers Pointers are used to: create more complex data types such as lists, queues, stacks or trees process arrays keep track of allocated memory returned by the new operator deallocate a block of memory by the delete operator Teresa Leyk Slide 2 C++ Review Pointers
Pointers (cont) To define a pointer variable we use the same rules as defining an ordinary variable. The only difference is that the asterisk is added in front of the name of the pointer variable. int i = 1; // define and initialize a variable i int *iptr; // define a pointer iptr iptr = &i; /* now value of iptr is the same as the address of i */ *iptr = 2; /* stores a value (2) in a variable pointed by iptr */ *iptr = *iptr + 1; // a new value of i is 3 The operator & is the address-of operator. The asterisk in *ptr is called the dereferencing (or indirection) operator. Teresa Leyk Slide 3 C++ Review Pointers
Pointers (cont) Pointers can point to nothing (NULL or 0) or to allocated memory by using the operator new. The operator new dynamically allocates the correct amount of memory for an object of a given type (from the free store) and returns a pointer to this object. Allocated memory can be returned to the free store by the operator delete. struct Student { }; int studentid; double studentgrade; Student *P = new Student; // a pointer to a structure P->studentID = 691001221; // initialize // (*P).studentID = 691001221; an equivalent notation P->studentGrade = 3.5; // initialize // (*P).StudentGrade = 3.5; is an equivalent notation delete P; // deallocate Teresa Leyk Slide 4 C++ Review Pointers
Pointers (cont) Computer memory is a one-dimensional array of bytes. Each object is stored in a contiguous sequence of bytes. The address of an object is the address of the first byte in this sequence. Let int *iptr = #. Then iptr value (address) 123200 refers to the location of the integer variable num and *iptr has the value 2. 12800 123200 address 123200... 2... data int *iptr int num object type To avoid run-time errors with pointers a programmer needs to remember to initialize them either to 0 or to an address of an existing object. Pointers in functions are not automatically initialized to 0. Teresa Leyk Slide 5 C++ Review Pointers
Pointers and References A reference is an automatically dereferenced pointer, or an alternative name for an object. Differences between pointers and references: to generate a pointer value you use the operator new or address-of (&) a pointer s value (address) can be changed assignment of pointers does shallow copy (only addresses are copied) int i = 1; int *pi = &i; int *pa = new int(10); pa = pi; // shallow copy to access an object pointed to by a pointer you use the operators * or [] *pi = 2; pi[0] = 3; Teresa Leyk Slide 6 C++ Review Pointers
Pointers and References (cont) a reference value (address) cannot be changed after its initialization int &ri = *pi; // now ri is an alias of *pi ri = pa; // illegal (ri is not a pointer) ri = *pa; // legal (*pa is a variable) a reference cannot be initialized to 0 int &rb = *0; // illegal pi = 0; // legal assignment of references does deep copy int j = 5; int i = 10; int &rj = j; int &ri = i; rj = ri; // value of j is now 10 Teresa Leyk Slide 7 C++ Review Pointers
Pointers and References (cont) The references are practically used only in function arguments (call-by-reference) and for returning an object from a function as a reference. Example. // swap values of x and y void swap(int& x, int& y) { } int tmp = x; x = y; y = tmp; Teresa Leyk Slide 8 C++ Review Pointers
The this Pointer The keyword this identifies a special type of pointer. Suppose that you create an object named g of class B, and class B has a member function funct(). If you call the function g.funct(), the keyword this in the body of funct() stores a pointer to (address of) g. You cannot declare the this pointer or make assignments to it (it is a constant pointer). In other words, the this pointer is a pointer that points to the object for which the member function is called. The this pointer is called implicitly every time we access a class member (that s why you do not see a lot of this in C++ programs). Only when we need to refer to the whole object, we need to mention this explicitly. Teresa Leyk Slide 9 C++ Review Pointers
The this Pointer (cont) Example (see pages 603 607). class Link { public: string value; Link(const string& v, Link *p=0, Link *s=0) : value(v), prev(p), succ(s) {} // constructor Link* insert(link* n); // insert n before this object // more Link functions are here private: Link *prev, *succ; }; Teresa Leyk Slide 10 C++ Review Pointers
The this Pointer (cont) Example (cont) // insert n before this object Link* Link::insert(Link* n) { } if (n==0) return this; n->succ = this; // this object comes after n if (prev!=0) prev->succ = n; n->prev = prev; prev = n; return n; Note that we could have used this->prev and this->succ in place of prev and succ, respectively, but it is not necessary, and it is not considered as a good programming style. Teresa Leyk Slide 11 C++ Review Pointers
The this Pointer (cont) Example (cont). It is how we invoke the insert function. Link* greek_gods = new Link("Hera"); // it inserts the string "Athena" before "Hera" greek_gods = greek_gods->insert(new Link("Athena")); // it inserts the string "Mars" before "Athena" greek_gods = greek_gods->insert(new Link("Mars")); // it inserts the string "Poseidon" before "Mars" greek_gods = greek_gods->insert(new Link("Poseidon")); The this pointer inside the insert function has the same value as the pointer greek_gods. Note that the pointer greek_gods is changed in each corresponding line so the this pointer is different in each call. Teresa Leyk Slide 12 C++ Review Pointers
Generic (type void*) Pointer The type void* pointer, also known as the generic pointer, is a special type of pointer that can be pointed at objects of any data type. A generic pointer is declared like a normal pointer, using the void keyword as the pointer s type: void* pgen; // pgen is a generic pointer A generic pointer can point to objects of any data type. The type void* means pointer to some memory that the compiler does not know the type of. We use void* when we want to transmit an address between pieces of code that really do not know each other s types. Examples: the address arguments of a callback function and lowest level of memory allocation (used in the implementation of the new operator). Teresa Leyk Slide 13 C++ Review Pointers
Generic (type void*) Pointer (cont) A pointer to any object type can be assigned to a void*. Examples: void* pv1 = new int; // int* converts to void* void* pv2 = new double[10]; // double* converts to void* When we want to access a value pointed by a generic pointer, we need to cast it to correct type (we need to know which type, otherwise the program will crash). Examples: int i = 10; void* pv = &i; // int* converts to void* int* pi = static_cast<int*>(pv); // explicit conversion *pi = -1; // the value of i is now -1 *pv = -1; // ERROR: cannot dereference a void* Teresa Leyk Slide 14 C++ Review Pointers