Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string Type
Pointer Syntax in C++ To have a pointer point at an object, the target object's memory address must be known For any object obj, its memory address is given by applying the unary address-of operator & Thus &obj is the memory location that stores obj We can declare that an object ptr points at an int object by saying int *ptr; The value represented by ptr is an address using ptr before assigning anything to it invariably produces bad results
Continued…. Suppose we have declaration int x=5; int y=7; make ptr point at x by assigning to ptr the memory location where x is stored. Thus ptr = &x; / / LEGAL The unary dereferencing operator * access the data through pointer To dereference something that is not a pointer is illegal The * operator is the opposite of & Dereferencing works not only for reading values from an object, but also for writing new values to the object *ptr = 10; // LEGAL changed the value of x to 10
Continued…. Declaration of pointer can be done as int x = 5; int y = 7; int *ptr = &x; / / LEGAL Wrong declaration int *ptr = &x; // ILLEGAL: x is not declared yet int x = 5; int y = 7;
Continued…. Wrong declaration int x = 5; int y = 7; int *ptr = x; // ILLEGAL: x is not an address Correct but not meaningful declaration int x = 5; int y = 7; int *ptr; // LEGAL but ptr is uninitialized
Continued…. A dereferenced pointer behaves just like the object that it is pointing at. So, following declaration will save x=15 x = 5; ptr = &x; *ptr += 10; Two more declaration of dereferenced operator *ptr +=1; *ptr ++; In the first statement the += operator is applied to *ptr, but in the second statement the ++ operator is applied to ptr
Continued…. If ptr1 and ptr2 are pointers to the same type, then following sets ptrl to point to the same location as ptr2 ptr1 = ptr2; Following assigns the dereferenced ptrl the value of the dereferenced ptr2 *ptr1 = *ptr2; Following is true if the two pointers are pointing at the same memory location ptrl == ptr2 Following is true if the values stored at the two indicated addresses are equal *ptr1 == *ptr2;
Null Pointer The NULL pointer points at a memory location that is guaranteed to be incapable of holding anything A NULL pointer cannot be dereferenced The symbolic constant NULL is defined in several header files, and either it or an explicit zero can be used Pointers are best initialized to the NULL pointer because in many cases they have no default initial values
Dynamic Memory Management Term automatic variable indicate local variables are created when they are reached in the function and that they are destroyed when they are no longer in scope Objects need to be created in a different way. This different way is called dynamic memory allocation
The new Operator Objects can be created dynamically by calling new The new operator dynamically allocates memory It returns a pointer to the newly created object
Garbage Collection and delete Operator In some languages, when an object is no longer referenced, it is subject to automatic garbage collection C++ does not have garbage collection When an object allocated by new is no longer referenced, the delete operator must be applied to the object Otherwise, the memory that it consumes is lost which is known as a memory leak Unfortunately memory leaks are common occurrences in many C++ programs Fortunately, many sources of memory leaks can be automatically removed with care
Continued…. One important rule is not to use new when an automatic variable can be used instead An automatic variable is automatically cleaned up The delete operator should never used on an object that was not created by new; if it is used, run-time havoc is likely to result