Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.

Similar presentations


Presentation on theme: "Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string."— Presentation transcript:

1

2 Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string Type

3 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

4 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

5

6 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;

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

8 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

9 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;

10 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

11 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

12 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

13

14 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

15 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


Download ppt "Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string."

Similar presentations


Ads by Google