Announcements and Reminders
|
|
RecordingComments on Assignment 3
Pointers ContinuedPointer Arithmetic
A pointer to pointer Other stuff Return
by address
pointer to const, const pointer Dynamic Memory AllocationThree types of memory in the computer
The new and delete operators
Examples:
int
*ptr;
ptr = new int; *ptr = 6; or int*
ptr = new
int(6);
Where is ptr stored? Where is the dynamic memory? Release the memory delete ptr;
More examples: float*
ptrPI =
new float(3.14);
... delete ptrPI; char* pc = new char; *pc = 'x'; ... delete pc; dog* pSirius = new dog; ... delete pSirius; Allocate memory for an array int
* pA = new
int[5];
for (int i = 0; i < 5; i++) pA[i] = 5 * i; ... delete [] pA; char* pName = new char[6]; strcpy(pName,"Harry"); ... cout << *pName; ... delete [] pName; Notes:
What does it mean, "to move a pointer"?
Another Example Joe's notes on dynamic memory allocation | |
Lab Exercise #4Put your name, the compiler used, and Lab Exercise #4 in a comment at the top of your program. Email your source code. This lab exercise is due at the beginning of the next lecture. Write a program that performs the following steps.
|