Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Pointers Read Chapter 2 (P. 63 - 75). COP3530 – C++ Pointers Pointers Pointers provide a method of referencing the memory location of variables provide.

Similar presentations


Presentation on theme: "C++ Pointers Read Chapter 2 (P. 63 - 75). COP3530 – C++ Pointers Pointers Pointers provide a method of referencing the memory location of variables provide."— Presentation transcript:

1 C++ Pointers Read Chapter 2 (P. 63 - 75)

2 COP3530 – C++ Pointers Pointers Pointers provide a method of referencing the memory location of variables provide a method of referencing the memory location of variables analogy in real world: analogy in real world: ATM card has a number on it that references your bank account – ATM card has a number on it that references your bank account – ordinary variable declaration ordinary variable declaration int value; compiler allocates memory compiler also associates name with the memory address compiler puts the associated value in slots (if necessary) thus, & (address operator) can be used to retrieve the address (slot number) of the variable

3 Pointers (cont.) Declaring Pointers Declaring Pointers format: format: type * pointer; type * pointer; - this creates a variable named pointer that can be used to contain the address of another var of the defined type example: int * MyPointer; int * MyPointer; - creates a pointer called MyPointer that can be used to point to another int - the pointer is bound to the type that it is defined for - can also use typedef to create a pointer type typedef int * IntPointer; typedef int * IntPointer; IntPointer MyPointer; IntPointer MyPointer; (creates a pointer of the IntPointer type called MyPointer)

4 Pointers (cont.) Assigning values (addresses) to Pointers Assigning values (addresses) to Pointers format: format: int x = 4; int x = 4; MyPointer = &x; MyPointer = &x; indirection/dereferencing indirection/dereferencing again, MyPointer is a pointer that points to a memory address of some variable, in this case x. again, MyPointer is a pointer that points to a memory address of some variable, in this case x. MyPointer is basically an alias for the variable it points to MyPointer is basically an alias for the variable it points to if I wish to know what value is located at an address pointed to by a pointer, I put an * before the pointer – if I wish to know what value is located at an address pointed to by a pointer, I put an * before the pointer – * is called the indirection operator and using it like this is called dereferencing the pointer * is called the indirection operator and using it like this is called dereferencing the pointer

5 Pointers (cont.) ex: ex: typedef int * intPointer; void main() { int Value1 = 10; int Value1 = 10; int Value2 = 20; int Value2 = 20; intPointer Pointer1; intPointer Pointer1; Pointer1 = &Value1; Pointer1 = &Value1; cout << "Value1 " << Value1 << endl; cout << "Value1 " << Value1 << endl; cout << "Pointer1 " << *Pointer1 << endl; cout << "Pointer1 " << *Pointer1 << endl; Pointer1 = &Value2; Pointer1 = &Value2; cout << "Value2 " << *Pointer1; cout << "Value2 " << *Pointer1; } What is output? –

6 Pointers (cont.) In the above example, if I wished to change the value of Value2, I could do so in two ways: In the above example, if I wished to change the value of Value2, I could do so in two ways: (old way) Value2 = 30; (old way) Value2 = 30; Or, I could use the pointer to change the value: Or, I could use the pointer to change the value: *Pointer1 = 30; - will reset the value that Pointer1 points to to 30.

7 Pointers (cont.) - Pointers and Classes - Can use pointers to point to class objects: Clock * ClockPtr; Clock * ClockPtr; ClockPtr ClockPointer; ClockPtr ClockPointer; ClockPointer = &Clock1; ClockPointer = &Clock1; - then you can easily pass objects to functions (will discuss later) - To reference a member function of an object pointer: (*ClockPointer).theHour(); (*ClockPointer).theHour(); - or use the class pointer selector operator: - or use the class pointer selector operator: ClockPointer ->theHour(); - will execute theHour() function of the Clock1 object (pointed to by ClockPointer)

8 Pointers (cont.) Assign Pointer to a Pointer int x = 4, y = 5; IntPointer Pointer1 = &x; intPointer Pointer2 = &y; Pointer1 = Pointer2; - both pointers now point to address of variable y - thus the following statement: cout << *Pointer1 << " " << *Pointer2 << endl; Will produce the following output: 5 5 (since derferencing both will give the value contained in y) - this is often difficult to debug - (the aliasing problem)

9 Why Use Pointers? / Pointers with function arguments - original versions of C did not have & operator with functions - if needed to pass variables by reference, you had to pass the address of the variable: void Doubleit(int &n) { n *= 2; n *= 2;} call: DoubleIt(x); Can be written: void Doubleit(intPointer Iptr) { *Iptr *= 2; *Iptr *= 2;} call: DoubleIt(&x); call: DoubleIt(&x);

10 Pointers and Arrays Pointers and Arrays when assigning arrays to pointers, the pointer will point to the starting point of the array when assigning arrays to pointers, the pointer will point to the starting point of the array char MyArray[10]; typedef char * CharPointer; CharPointer myPtr; myPtr = MyArray; //will point to slot 0 of MyArray

11 Pointers (cont.) Other operations Other operations comparisons: comparisons: can use regular relational ops to see if two pointers point to same memory space: can use regular relational ops to see if two pointers point to same memory space: if (Pointer1 == Pointer2) //true if point to same address if (Pointer1 == Pointer2) //true if point to same address if (Pointer1 != Pointer2) // true if they point to different if (Pointer1 != Pointer2) // true if they point to different arithmetic: arithmetic: can add to/subtract from pointers: can add to/subtract from pointers: intPointer Pointer1; intPointer Pointer1; Pointer1++; Pointer1++; - will add the size of that pointer to the pointer (move to next slot in memory)

12 More Pointer Operations: int myInts[10]; IntPointer myPointer; myPointer = MyInts; //points to slot 0 for (int i = 0; i < 10; i++) { *myPointer = 0; *myPointer = 0; myPointer++; myPointer++;} - will initialize entire array to 0's

13 Dynamic Memory and Pointers - in all previous examples, memory is allocated for variables when program is compiled - programmer is forced to know the size of memory needed when the program is written; - not always efficient dynamic allocation: dynamic allocation: allowing the program to determine the size of memory needed at run-time allowing the program to determine the size of memory needed at run-time two steps: two steps: 1) acquire the memory when needed (allocate) 1) acquire the memory when needed (allocate) 2) discard the memory when done (deallocate) 2) discard the memory when done (deallocate)

14 Dynamic Memory (cont.) Acquiring memory with new operation - new: Acquiring memory with new operation - new: used to request additional memory when program is running used to request additional memory when program is running format: format: new VariableType; new VariableType; ex: ex: int * intPtr; int * intPtr; intPtr = new int; intPtr = new int; - requests a size in memory big enough to hold an int - if able, intPtr will be assigned the adress of the slot; - if unable, the null address (0) is assigned - If the null adress is not assigned intPtr is said to be an anonymous variable (has no name assigned to it) - Can access the variable by using dereferencing: cin >> *intPtr; *intPtr++; cin >> *intPtr; *intPtr++;

15 Dynamic Memory (cont.) To use new with an array: To use new with an array: new VariableType[ArraySize]; new VariableType[ArraySize];Ex: int TotalItems; cout << “Enter # of values to store:”; cin >> TotalItems; intPtr MyArray; MyArray = new int[TotalItems]; for (int i = 0; i < TotalItems; i++) { cout << “Enter Value: “; cout << “Enter Value: “; cin >> MyArray[i]; cin >> MyArray[i];}

16 Dynamic Memory (cont.) The Delete operator The Delete operator Used to remove data from the heap when no longer needed Used to remove data from the heap when no longer needed Heap also called runtime stack Heap also called runtime stack Delete: Delete: delete Pointer; delete Pointer; delete [] ArrayPointer; delete [] ArrayPointer;Ex: int * iptr; char * cptr; iptr = new int; cptr Memory; delete iptr; Memory = new char[100]; delete [ ] Memory; delete [ ] Memory;

17 Dynamic Memory- Deconstructors class Person { public: Person(); Person(); Person(char * N, int A); Person(char * N, int A); void Display(); void Display();private: char * Name; char * Name; int Age; int Age;};

18 Dynamic Memory – Deconstructors Person::Person(){ Name = 0; Age = 0; } Person::Person(char * N, int A) { int Len; Len = StrLen(N); Name = new char[Len + 1]; Strcpy(Name, N); Age = A; }

19 Dynamic Memory – Deconstructors Program allocates memory at runtime for each object declared: Program allocates memory at runtime for each object declared: Person P1(“Jim Jones”, 30); Person P2(“Steve Snapper”, 25); Make sure space is deallocated using deconstructor: Make sure space is deallocated using deconstructor:~Person();Person::~Person(){ delete [ ] Name; } Automatically removes object from heap when lifetime of that object is exhausted Automatically removes object from heap when lifetime of that object is exhausted

20 QUESTIONS? Prepare for TEST #1 Prepare for TEST #1 Chapter 1 of Book (Notes) Chapter 1 of Book (Notes) Review of Classes (Ch. 4 in Book and Notes) Review of Classes (Ch. 4 in Book and Notes) ADTs (Lists) (Ch. 6 in Book and Notes) ADTs (Lists) (Ch. 6 in Book and Notes) Pointers (Ch. 2 in Book and Notes) Pointers (Ch. 2 in Book and Notes) ONLINE HELP SESSION: ONLINE HELP SESSION: Tues. July 4 7:00 a.m. – 8:00 a.m. (tentatively) Tues. July 4 7:00 a.m. – 8:00 a.m. (tentatively) Log on to Yahoo! Instant Messenger (I will send you an INVITE to join a conference when I’m online) Log on to Yahoo! Instant Messenger (I will send you an INVITE to join a conference when I’m online)


Download ppt "C++ Pointers Read Chapter 2 (P. 63 - 75). COP3530 – C++ Pointers Pointers Pointers provide a method of referencing the memory location of variables provide."

Similar presentations


Ads by Google