7/27/2019 Dynamic Object
1/23
Review on pointers and
dynamic objects
7/27/2019 Dynamic Object
2/23
Memory Management
Static Memory Allocation
Memory is allocated at compiling time
Dynamic Memory Memory is allocated at running time
{int a[200];
}
{int n;
cin >> n;
a[n]???
}
7/27/2019 Dynamic Object
3/23
Static vs. Dynamic Objects
Static object
Memory is acquired
automatically
Memory is returned
automatically when objectgoes out of scope
Dynamic object Memory is acquired by
program with anallocation request
new operation Dynamic objects can
exist beyond the functionin which they wereallocated
Object memory is
returned by a deallocationrequest
delete operation
7/27/2019 Dynamic Object
4/23
Why pointers?Dynamic objects are implemented or realized by
pointers which are parts of low-level physicalmemory
We dont like it, but can not avoid it.
Low level languages directly manipulate them
High level languages want to hide the pointers
(conceptually remove them)
7/27/2019 Dynamic Object
5/23
Pointers
A pointer is a variable used for storing the address ofa memory cell.
We can use the pointer to reference this memory cell
100 1024 Memory address: 1024 10032
1020Integer a Pointer p
int a;
int* p;
a
7/27/2019 Dynamic Object
6/23
Getting an address: address operator &
int a=100; &a the address of a
100 Memory address: 1024
int a = 100;
cout
7/27/2019 Dynamic Object
7/23
Dereferencing Operator*
We can access to the value stored in the variable
pointed to by preceding the pointer with the staroperator (*),
10088 1024 Memoryaddress: 1024 10032 1020
int a = 100;
int* p = &a;
cout
7/27/2019 Dynamic Object
8/23
Pointer to pointer
int a;
int* p;
int** q;
a = 58;
p = &a;
q = &p;
a p q
58
a, *p, and **q are the same object whose value is 58!
But q = &a is illegal!
7/27/2019 Dynamic Object
9/23
An asterisk (*) has two usages
In a definition, an asterisk indicates that the
object is a pointer.
char* s; // s is of type pointer to char
(char *s; is possible)
In expressions, an asterisk before a pointer
indicates the object the pointer pointed to,
called dereferencing
int i = 1, j;int* ptr; // ptr is an int pointer
ptr = &i; // ptr points to i
j = *ptr + 1; // j is assigned 2
cout
7/27/2019 Dynamic Object
10/23
Writing pointer type properly
in C++
int *a, *b;
a, b are both integer
pointers
int* a, b;
a is integer pointer, b is
just integer!
typedefine int* IntPt;
IntPt a, b;
typedefine int MyInt;
MyInt k;int k;
int* a;
int* b;?
Recommended!!!
I dont like this!
7/27/2019 Dynamic Object
11/23
Summary* has two usages:
- pointer type definition: int a;
int* p;
- dereferencing: *p is an integer variable if
p = &a;
& has two usages:
- getting address: p = &a;- reference: int& b a;
b is an alternative name for a
First application in passing parameters (swap example)
int a=10;int b=100;
int* p;
int* q;
P = &a;
Q = &b;
p = q;
*p = *q;
?
?
7/27/2019 Dynamic Object
12/23
Pointers and
References
Reference (implemented as a (const) pointer) is an abstraction,
Not available in C, only in C++.
7/27/2019 Dynamic Object
13/23
A pointer can be assigned a new value to point at a different object, but areference variable always refers to the same object. Assigning a reference
variable with a new value actually changes the value of the referred object.
int* p;
int m = 10;
int& j = m; //valid
p = &m; //p now points at m
int n = 12;
j = n; //the value of m is set to 12. But j still refers to m,not to n.
cout
7/27/2019 Dynamic Object
14/23
A reference variable is different from a pointer
int x=10;
int* ref;
Ref = &x;
int x=10;
int& ref = x;
x
ref
10
int& ref;
10x
ref
7/27/2019 Dynamic Object
15/23
Traditional Pointer Usage
void swap(char* ptr1, char* ptr2){
char temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
}int main() {
char a = 'y';
char b = 'n';
swap(&a, &b);cout
7/27/2019 Dynamic Object
16/23
Pass by Reference (better than
pointers)
void swap(char& y, char& z) {
char temp = y;
y = z;
z = temp;
}int main() {
char a = 'y';
char b = 'n';
swap(a, b);
cout
7/27/2019 Dynamic Object
17/23
Pointers and Arrays
Double faces of an array: int a[10]
a is the name of an array,
a is also is a constant pointer to its first element
7/27/2019 Dynamic Object
18/23
Pointers and Arrays
The name of an array points only to the first element not the whole array.
2
4
8
6
22a[4]
a[0]
a[2]
a[1]
a[3]
a
7/27/2019 Dynamic Object
19/23
Result is:
2
2
2
#include
Using namespace std;
void main(){int a[5] = {2,4,6,8,22};
cout
7/27/2019 Dynamic Object
20/23
To access an array, any pointer to the first elementcan be used instead of the name of the array.
We could replace *p by *a
#include Using namespace std;
void main(){
int a[5] = {2,4,6,8,22};
int* p = a;
int i = 0;
cout
7/27/2019 Dynamic Object
21/23
dynamic objects
7/27/2019 Dynamic Object
22/23
Static variables (objects) Dynamic variables (objects)
A (direct) named memory location A static part (pointer) + (indirect)nameless memory location
(dynamic part)int a;
a = 20;int* pa;
pa = new int;
*pa = 20;
20a 20pa
staticstatic
dynamic
Summary
7/27/2019 Dynamic Object
23/23
int* p = new int;
*p = 10;
delete p;
p 10 p 10
int* p = new int[100];
for (i=1;i