Date post: | 30-Dec-2015 |
Category: |
Documents |
Upload: | charles-burke |
View: | 131 times |
Download: | 10 times |
Data Structures in C++Pointers &
Dynamic Arrays
Shinta P.
Static and Dynamic Memory
• Static Allocation – allocated by the compiler at compile time– once allocated, does not change
• Dynamic Allocation– allocated by program at run time– ‘new’ allocates desired amount from heap
• amount depends on class/type– ‘delete’ deallocates an object and returns to
storage manager for reallocation
Where or When
• Static– data stricture sizes are fixed– little chance for storage needs to grow– prototypes
• Dynamic– amount of data changes from run to run– data relationships change frequently
Pointers
• a built-in primitive type; 32 bit• used to hold the storage address of a variable• to define a pointer variable
– use the * operator in the definition
eg. int *airplane_ptr ;eg. int *airplane_ptr ;
(airplane_ptr is a variable that will point to (airplane_ptr is a variable that will point to an integer)an integer)
Pointers (cont.)
• To assign a value to the pointer variable– use the address operator &
eg. int F15;eg. int F15;
int Airplane_ptr;int Airplane_ptr;
Airplane_ptr = &F15;Airplane_ptr = &F15;
F15F15Airplane_ptrAirplane_ptr
Pointers (cont.)
• Note that F15 has not been assigned a value yet
• to do this we must use the dereferencing operator *
ex. *Airplane_ptr = 5ex. *Airplane_ptr = 5
(dereferencing * reads: location pointed to by var)(dereferencing * reads: location pointed to by var)
F15F15Airplane_ptrAirplane_ptr
55
Int nilai=50;int *pnilai;cout<<nilai; // cetak 50cout<<&nilai; // 123fpnilai=&nilai; // pointer pnilai menunjuk alamat nilaicout<<pnilai; // 123fcout<<*pnilai; // 50
nilai =50
Alamat=123f , 4byte
*pnilai=123f
Alamat=421f , 4byte
• char head;• char data1=‘A’;• char data2=‘B’;• head= data1;• head= data2;• head= ‘C’;• cout<<data1;• cout<<data2;
char *head;char data1=‘A’;char data2=‘B’;head=&data1;head= &data2;*head= ‘C’;cout<<data1;cout<<data2;
Without pointer
With pointer
• char head;• char data1=‘A’;• char data2=‘B’;• head= data1;• head= data2;• head= ‘C’;• cout<<data1;• cout<<data2;
char *head;char data1=‘A’;char data2=‘B’;head=&data1;head= &data2;*head= ‘C’;cout<<data1;cout<<data2;
Without pointer
With pointer
B
A Data1
Data2
HeadDisplay:
AB
• char head;• char data1=‘A’;• char data2=‘B’;• head= data1;• head= data2;• head= ‘C’;• cout<<data1;• cout<<data2;
char *head;char data1=‘A’;char data2=‘B’;head=&data1;head= &data2;*head= ‘C’;cout<<data1;cout<<data2;
Without pointer
With pointer
C
A Data1
Data2
HeadDisplay:
AB
Display:AC
Pointers (cont.)
• Which is exactly equivalent to:
F15 = 5;F15 = 5;
...so whats the big deal??...so whats the big deal??
The Big Deal....
• We’ve been looking at the trivial case• Pointers to primitives aren’t very useful
– things get more interesting with arrays• we can make :
– an array that grows as the application needs more room for data
– an array that shrinks as the application needs less room for data
– and much better with dynamic objects
p1
More Pointers
int i = 50;int i = 50;
int j = 75;int j = 75;
int *p1 ; int * p2 ;int *p1 ; int * p2 ;
p1 = &i ; p2 = & j;p1 = &i ; p2 = & j;
cout << *p1;cout << *p1;
p1 = p2 ; *p2 =0;p1 = p2 ; *p2 =0;
cout <<*p1;cout <<*p1;
50 75
p2p1
i j
More Pointers
int i = 50;int i = 50;
int j = 75;int j = 75;
int *p1 ; int * p2 ;int *p1 ; int * p2 ;
p1 = &i ; p2 = & j;p1 = &i ; p2 = & j;
cout << *p1;cout << *p1;
p1 = p2 ; *p2 =0;p1 = p2 ; *p2 =0;
cout <<*p1;cout <<*p1;
50 0
p2p1
i j
Display:500
int i = 50;int i = 50;
int j = 75;int j = 75;
int *p1 ; int * p2 ;int *p1 ; int * p2 ;
p1 = &i ; p2 = & j;p1 = &i ; p2 = & j;
cout << *p1;cout << *p1;
p2 = p1 ; *p2 =0;p2 = p1 ; *p2 =0;
cout <<*p1;cout <<*p1;
0 75
p2p1
i j
Display:500
Pointers to arrays
• The name of an array is a pointer to the 0th element of the array (the beginning of the array)
int array[5] ;
// array is equivalent to & array[0]
*array = 5; is like array[0] = 5;
int j = *(array+4) is like int j = array[1]
cout << *array; is like cout << array[0];
Pointers to arrays
• Pass by reference - in C++ arrays are always pass by reference (there is no pass by value for an array)
• this is a big improvement over C– in C to pass an array to a function it had to be
passed by passing a pointer to the beginning of the array then doing pointer arithmetic to manipulate the contentsd of the array
new
• returns the address of a piece of dynamically allocated storage
ii
7575
ex. int *i; //create a pointerex. int *i; //create a pointer
i = new int // get a new integeri = new int // get a new integer
*i = 75 // assign it a value*i = 75 // assign it a value
Dynamic Arrays
• arrays can be allocated at run time
double * p;double * p;
int count ;int count ;
cout << “how many elements? “ << “\n”;cout << “how many elements? “ << “\n”;
cin >> count;cin >> count;
p = new double[count];p = new double[count];
Dynamic Arrays• You can effectively change the size of an array at run-time if it was originally
allocated dynamically.
… from previous example
double * temp;
temp = new double[20];
/* copy the contents of the old array into the new one */
for (int I=0 ; I < 10 ; I++)
temp[I] = p[I];
/* dispose of the original array */
delete p;
p = temp; /* now the array has twice as many elements */
Value semantics• The value semantics of a class determine how values are copied from one object to another.• In C++ the value semantics consist of two operations:
– the assignment operator– the copy constructor
• The copy constructor is a constructor that creates and initializes an object to the value of another (existing) object
– the copy constructor has one parameter whose type is the same as the class name
Ex.Ex.
Date Today;
Today.month=5 ; Today.year=2000; Today.day = 21;
Date Tomorrow(Today)
Copy Constructor
Date :: Date(const & Date t)
{
month = t.month;
day = t.month;
year = t.year;
}