pointers
DESCRIPTION
Pointers. Dynamic Memory. MEMORY MANAGEMENT. STATIC MEMORY Until now, we have only learned about static memory Memory is allocated at compilation time Declaration of large memory variables (arrays) must be determined before the program is compiled. int students[10]; - PowerPoint PPT PresentationTRANSCRIPT
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson Learning, 2000.
Pointers
Dynamic Memory
MEMORY MANAGEMENT
STATIC MEMORY
•Until now, we have only learned about static memory•Memory is allocated at compilation time•Declaration of large memory variables (arrays) must be determined before the program is compiled.
• int students[10]; • what if we don’t know the exact number of students in advance?
DYANMIC MEMORYWe can “ask” for memory while the program is running.
We can allocate and release memory ourselves!More powerful programming ability (also more potential for errors!)
Memory Allocation
{ int a[200]; int *ptr = a; …}
int *ptr = new int[200];…delete [] ptr;
Conceptual View of Memory
(DYNAMIC MEMORY)
Static vs. Dynamic Objects Static object
Memory is acquired automatically
Memory is returned automatically when object goes out of scope
Dynamic object Memory is acquired
by program with an allocation request
new operation Dynamic objects can
exist beyond the function in which they were allocated
Object memory is returned by a deallocation request
delete operation
Dynamic Memory
C++ “new” keywordnew <type>; // allocate size of the type new <type>[size]; // allocate an array
Asks the Operating System to return you a pointer to a “chunk” of data.
Allocating MemoryOperating System
Manages dynamicmemory.
You have:
int *p;
p = new int[1000];
Ask OS to find a segmentOf memory of 1000*4 btyes
OS returnsthe address.
So, you need a pointer to the type you requested.If you request <int>, you need an <int> pointer.
If you request <float>, you need a <float> pointer.
Allocating memory using “new”
8002
8006
8010
8014
8018
8022
8026
new returns the address to the“start” of the memory it allocated
int *p;p = new int[7];//p is assigned 8002
p[0] = *(p) = *(p+0) = ?p[1] = *(p+1) = ?p[2] = *(p+2) = ?
p[0]
p[1]
p[2]
Dynamic Memory Allocation
Request for “unused” memory from the Operating System
int *p, n=10;
p = new int;
p = new int[100];
p = new int[n];
pnew
pnew
pnew
Dynamic Memory Allocation Example
Need an array of unknown sizeint main(){ cout << “How many students? “; cin >> n;
int *grades = new int[n];
for( int i = 0; i < n; i++) { cout << “Input Grade for Student” << (i+1) << “ ? :”; cin >> grades[i] ;
} . . . // call a function with dynamic array printMean( grades, n ); . . . }
Dynamic Memory
C++ “delete” keyword
delete address; // delete one element at address
delete [] address; // delete array at address
How can we specify what the address is?We have pointer. Pointer variables storeaddresses.
Freeing (or deleting) Memory
The memory you request by “new” can not be used again until you explicitly release it via “delete”. We call this “freeing” or “releasing” or “deleting” memory.
int *p;p = new int [1000];delete [] p; // delete (or free) the array at p
NOTE, the value of p will not change! However, it is now no longer points to a valid memory address.
Freeing (or deleting) Memory
Freeing (or Deleting) memory “ delete” is a keyword
delete <address, i.e. pointer variable>; // a variable
example: int *p= new int; delete p;
delete [] <address, i.e. pointer variable>; // an array
example: int *p = new int[1000]; delete [] p;
Dynamic Memory Dynamic Memory is very powerful
C++ trusts you as the programmer to manage you own memory
While this gives you lots of control, it is easy to make mistakes.
Dynamic memory bugs is common in large software projects written in C++.
The Dangling Pointer problem
Be careful when you delete memory pointed to by p, you are not erasing a location that some other pointer q is pointing to.int *p, *q;P = new int;q = p;delete p;// q is a dangling pointer
p
qint
p
qint delete p
Memory Leak Problem
Make sure to delete memory when finished
int *p;
p = new int[100];
int a;
NO ONE HAS ACCESS TO THIS MEMORYNO ONE HAS ACCESS TO THIS MEMORY 100 integersp = &a;
aa
100 integers
aa
This memory is un-addressableAnd it is reserved until yourprogram terminates. It is garbage(it has leaked from the system)
Exercise 1
What is wrong with the following program?.
int main( ){ int x =20; int* p; p = new int; *p = 30; cout << *p << " " << x << endl; delete p; p = &x; delete p; cout << *p << " " << x << endl;
return 0; }
Exercise 2 What is wrong in the following code? int main( ){
const int SIZE = 10000000;int i;int A[SIZE];
for(i=0; i<SIZE; i++)A[i] = i;
int *B;B = new int[SIZE];
for(i=0; i<SIZE; i++)B[i] = A[i];
for(i=0; i<SIZE; i++)cout << A[i] << " " << B[i] << endl;
return 0; }
Exercise 3What is the output of the following program? int main( ){
int i, *p, *r, t[4]={0,1,2,3};
p = new int[4]; for(i=0; i<4; i++) p[i] = 6*(i+1); r = t; for(i=3; i>=0; i--) r[i] -= 1; r[2] = 8; for(i=0; i<4; i++) t[i] = p[i] + r[i]; for(i=0; i<4; i++) cout << t[i] << " "; delete [] p; return 0;
}
Exercise 4
What is the output of the following program? void F1(int* temp){ *temp = 99; } void main( ){
int *p1, *p2;p1 = new int;*p1 = 50;p2 = p1;F1(p2);cout << *p1 << " " << *p2 << endl;p1 = new int;*p1 = 88;cout << *p1 << " " << *p2 << endl;delete p1;delete p2;
}
Exercise 5
What is wrong with the following program?
int main( ){int *p, *r;p = new int;r = new int;p = 1;r = p;
return 0;}