Académique Documents
Professionnel Documents
Culture Documents
All
Allocation
i
COL 100 - Introduction to Computer
p
Science
II Semester 2015-2016
Department of Computer Science and Engineering
Indian Institute of Technology Delhi
April 2016
class Employee {
int details [10000];
public:
bli
void print ();
};
void f () { // use t1
}
void g () { // print details
t1 print ();
t1.print
}
int main () {...
f (); // use t1
g (); // use t1
h (); // dont need t1 any more
}
Objects
b
Sometimes Waste Space
class
l
E
Employee
l
{
int details [10000];
public:
void print ();
} t1; // global object
void f () {{...}}
void g () {t1.print ();}
int main () {...
f ();
() // use t1
g (); // use t1
h (); // dont need t1 any more
}
class Employee {
int details [10000];
public:
void print ();
};
void f (Employee &t) {...}
void
id g (Employee
(E l
&t) {t1
{t1.print
i t ()
();}}
int main () {
Employee
p y t1; // local to main
f (t1); // use t1
g (t1); // use t1
h (); // dont
don t need t1 any more
}
Better Design
I both
In
b th cases, t1 occupies
i memory even after
ft it iis useless
l
(d
(during
i h())
April 2016
April 2016
Define Appropriate
pp p
Scopes
p
Enclose in tight
scope
t1 de-allocated
de allocated
(freed) in
stack after end
of scope
April 2016
class Employee {
int details [10000];
public:
bli
void print ();
};
void f (Employee &t) {...}
void g (Employee &t) {...}
int main ()
{
int a;
{
Employee t1; // local to main
f (t1); // use t1
g (t1); // use t1
}
h (); // dont need t1 any more
}
class Employee {
int details [10000];
public:
void print ();
};
void
id f (Employee
(E l
&t) {{...}}
void g (Employee &t) {...}
int main ()
{
Employee t0;
{
Employee t1;
f (t1); g (t1);
t3
}
{
Employee t2;
t0 t0
f (t2); g (t2);
}
Employee t3;
h ()
(); // dont
d t need
d t1,t2
t1 t2
} Delhi
(C) P. R. Panda, IIT
Early
y DeDe-allocation
Memory space
occupied by t1
can be
b re-used
d
for t2, t3
Memory
Occupied
t0
April 2016
t1
t2
t0
t0
time
April 2016
class Employee {
int details [10000];
public:
void print ();
}
};
void f () {...}
void g () {t1.print ();}
int main () {...
if (cond) {
f ()
(); // t1 created conditionally
y
} else {p ();}
if (cond) {
g (); // access t1 if it exists
} else {q ();}
h (); // dont need t1 any more
}
(C) P. R. Panda, IIT Delhi
class
l
E
Employee
l
{
int details [10000];
public:
void print ();
};
void f () {{...}}
void g () {t1.print ();}
int main () {...
if (cond) {
// CREATE t1 HERE
f ();
} else {p ();}
if (cond) {
g ();
// FREE t1 HERE
} else {q ();}
h (); // dont need t1 any more
}
April 2016
class Employee {
int details [10000];
public:
void print ();
};
int main () {
Employee t1;
Employee
p y *ep;
p;
ep = &t1;
}
100
104
t1.id
t1
t1 name
t1.name
204
100
ep
Employee *ep;
ep = &t1;
}
April 2016
10
April 2016
built-in types
yp ((int, char,...))
user-defined types (class, struct, array,...)
to p
pointer type
yp
to functions!
byte
y address for bool type
yp
11
Pointer Dereferencing
VALUE accessed through
DEREFERENCING
operator *
a
x
100
104
100
104
100
104
3
100
a
x
100
104
a
x
2
100
a
x
12
Dereferencing a Pointer to
Struct/Class
Struct
/Class
-> operator same as
D f
Dereferencing
i +
Member
*a.b abbreviated to
a->b
April 2016
class Employee {
public:
p
int id;
char name [100];
};
int main () {
Employee t1;
Employee *ep
ep = &t1;
ep->id = 10;
cout << t1.id; // 10
}
13
Pointer to Pointer
Pointers can be
composed in terms
pointers
of other p
x is pointer to int
y is
i pointer
i t tto
pointer-to-int
April 2016
int main () {
int a = 2;;
int *x = &a;
100
104
108
2
100
104
a
x
y
14
100
104
108
112
116
2
100
100
a
x[0]
x[1]
[1]
x[2]
x[3]
15
Pointer to an Array
p = a means p points to a[0]
int main () {
i t a[5];
int
[5]
int *p = a;
*p
p = 3; // a[0]
[ ]=3
}
April 2016
100 104
104
108
112
116
120
p
a[0]
a[1]
a[2]
a[3]
a[4]
100 104
104
3
108
112
116
120
p
a[0]
a[1]
a[2]
a[3]
a[4]
16
Pointer Arithmetic
p++ means p = p + sizeof (int)
int main () {
i t a[5];
int
[5]
int *p = a;
*p = 3; // a[0] = 3
p++;
}
April 2016
100 104
104
3
108
112
116
120
p
a[0]
a[1]
a[2]
a[3]
a[4]
100 108
104
3
108
112
116
120
p
a[0]
a[1]
a[2]
a[3]
a[4]
17
Pointer Arithmetic
*p++ = 4 means
p 4 , then p
p = p + sizeof (int)
(int)
*p=4,
int main () {
i t a[5];
int
[5]
int *p = a;
*p = 3; // a[0] = 3
p++;
*p++ = 4; // a[1] = 4
}
April 2016
100 108
104
3
108
112
116
120
p
a[0]
a[1]
a[2]
a[3]
a[4]
100 112
104
3
108
4
112
116
120
p
a[0]
a[1]
a[2]
a[3]
a[4]
18
Pointer Arithmetic
*(p+2) = 6 means set content of
address p
p+2
2*sizeof
sizeof (int)
(int) to 6
int main () {
i t a[5];
int
[5]
int *p = a;
*p = 3; // a[0] = 3
p++;
*p++ = 4; // a[1] = 4
*(p+2)
(p ) = 6;; // a[4]
[ ]=6
}
April 2016
100 112
104
3
108
4
112
116
120
p
a[0]
a[1]
a[2]
[2]
a[3]
a[4]
100 112
104
3
108
4
112
116
120
6
p
a[0]
a[1]
a[2]
a[3]
a[4]
19
Pointer Arithmetic
q = p+4 means
= (Start
q
(St t address
dd
off t1)
+ 4 * sizeof (Employee)
April 2016
class Employee {
public:
p
int id;
char name [100];
};
int main () {
Employee t1 [10];
Employee *p
p = t1;
Employee *q = p + 4;
q->id = 10; // t1[4] = 10
}
20
int main () {
intt a[
a[10];
0];
int *p = a;
a [3] = 5;
cout << p[3]; // 5
cout << *(p+3); // 5
}
a = p would be wrong
a is const Pointer
April 2016
21
Constant Pointer vs
Pointer to Constant
Pointer to Constant: const int *p;
*p = 10; // not allowed
p = &a; // allowed
Resolve confusion?
Read from Right to Left!
April 2016
22
April 2016
23
Identical
void f (int a [ ]) {
a[i] = 3;
*(a+i) = 3;
}
int main () {
int x [10];
f (x);
( )
}
April 2016
24
Identical
int f (const int *a, int j) {
return a [j];
}
int main () {
int x [10];...
cout << f (x, 4);
}
April 2016
25
int main () {
int x;
int y [10];
int *p;
p = &x; // points to x
p = y; // points to y[0]
p = &y; // also points to y[0]!
}
OK because
interpretation is
address. *p means:
point to scalar
point to array element
April 2016
26
April 2016
Identical
void f (int a [ ]) {
a[0] = a[0] + 4;
}
int main () {
int x [10];
f (x);
int y = 2;
f (&y);
}
27
Call by Reference
NOTE: Pointer can be
used
d tto iimplement
l
t
Pass-by-Reference
standard in C
language
In C++,
C
& parameter
is cleaner
Pointer obfuscates
syntax
April 2016
Identical
void f (int &a) {
a += 4;
}
int main () {
int y = 2;
f (y);
}
C++
C
28
So...Why Pointers?
In C++, not really needed for Call-byReference
Useful in Dynamic Memory Allocation
April 2016
29
OS finds memory,
returns POINTER
class Employee {
public:
int id;
int details [10000];
void p
print ();
};
int main () {
int x [3];
Employee *e;
e = new Employee;
e->id
id = 5;
5
e->print ();
sizeof (Employee)
}
April 2016
30
}
April 2016
Employee
details
e = new Employee;
e->id
e
>id = 5;
e->print ();
id
800
804
204
208
212
216
800
x[2]
x[1]
x[0]
e
main
31
800
804
10
details
204
800
800
Employee
create_emp:
t
f
main: e
f has disappeared
but the object survives
32
800
804
id
details
e = new Employee;
e->id = 5;
e->print ();
delete e;
// memory no longer used
}
(C) P. R. Panda, IIT Delhi
204
208
212
216
800
x[2]
x[1]
x[0]
33
April 2016
800
804
id
details
900
904
id
details
204
208
212
216
900
x[2]
x[1]
x[0]
34
id
Employee
name
id
name
id
name
212
216
(C) P. R. Panda, IIT Delhi
800
all
n
main
35
April 2016
Employee
name
id
name
id
name
id
212
216
800
all
n
main
36
April 2016
37
38
Dynamically
y
y Allocating
g 2D Arrays
y
int ** f (int rows, int cols) {
int **b;
b = new (i
(intt *)[rows]
*)[
]
for (int i = 0; i < rows; i++) {
b[i] = new int [cols];
}
return b;
}
could be of
different sizes
Storage NOT
identical to 2D array
more general
structure
April 2016
void g() {
int a [5][6];
int **c = f (5,6);
a[3][2] = 7;
c[3][2]
[3][2] = 7
7;
}
b[3][2]
7
b[3]
39
Dynamic
D
i 2D array?
?
address = ?
Store at (address)
Advantage of dynamic
2D a
arrays?
ays
April 2016
b
void g() {
int a [5][6];
int **c = f (5,6);
a[3][2] = 7;
c[3][2]
[3][2] = 7
7;
}
b[3][2]
7
b[3]
40
De--allocating
De
g 2D Arrays
y
Reverse
sequence:
first
fi d
delete
l
allll
int arrays
finally delete
int* array
Why different
sequence?
b[3][2]
void g() {
int a [10][20];
int **c = f (10,20);
g (c);
}
b[3]
April 2016
41
April 2016
42
April 2016
43
Dynamic Allocation in
Cl
Class
Constructors
C
tu t
Constructor
parameters
passed
d as iin
normal objects
April 2016
44
Dynamic Allocation in
Cl
Class
Constructors
C
tu t
Variable-sized
Objects can be
allocated
ll
t d iin
Constructor
instead of
using
g fixed
array size
((details [[1000])
])
April 2016
45
Class Destructors
Object deletion
done in Class
DESTRUCTOR
Called when object
j
goes out of scope
e.details
e details deleted at
end of function f
April 2016
46
Class
D tu t
Destructors
Destructor also
called when
object is explicitly
deleted (p)
Only objects
allocated via new
(details) need
deletion
no need to delete
member arrays (t)
April 2016
class
l
E
Employee
l
{{public:
bli
int id;
int t [10];
int *details;
void print ();
Employee
p y ((int i, int n)) {{...}}
}
~Employee () {delete [ ] details;}
};
Employee *f () {
Employee e (10, 5);
return new Employee (6,40);
(6 40);
}
int main () {
Emplo ee *p = f ();
Employee
()
delete p;
}
(C) P. R. Panda, IIT Delhi
47
48
Dynamic data?
variable size
April 2016
49
Heap
p shared
among all programs
Program P1
Heap
Stack
Program P1
Program P2
Global
Code
(simplified view)
April 2016
50
Programming problems
dereferencing invalid pointers
April 2016
51
int main () {
int a = 2;
int *x;
*x
x = 3;
}
100
104
int main () {
int a = 2;
int *x; ...
if (a) x = &a;
* =3
*x
3;
}
2
a
x
Tries to write 3 to
garbage address
April 2016
52
Reference to dead
j
objects
unexpected values taken
by unrelated variables
address remembered in x
even though variable is
dead
104
April 2016
100
104 100
b
x
104 100
100
104 100
c
x
100 3
104 100
53
c
x
April 2016
...
int main ()
{
Employee *x = new Employee;
...
delete x;
...
x->id = 5; // Danger. Freed object
}
54
Memory Leaks
Memory leaked
if not freed after
allocation
may lead to run
runtime out-ofmemory errors
memory
causing system
crash
h
April 2016
...
int main ()
{
Employee *x;
int n;
for (int i = 0; i < 1000; i++) {
cin >> n;
x = new Employee [n];
// x not de
de-allocated
allocated
}
}
Objects allocated
but never freed
55
56