Lecture 13
Lecture 13
Algorithms 1
CHAPTER 6: DYNAMIC MEMORY
ALLOCATION (INTRODUCTION)
Sep – Dec 2023
Introduction
Motivation:
What is a pointer?
a memory address!
a variable that store memory address!
Introduction to Pointer
address of y
is value of
yptr
Pointer Operators
* (indirection/dereferencing operator)
The value of a is 7
The value of *aPtr is 7
int main() { a b c
int a = 4;
int b = 8;
4 8 -3
0x12 0xab 0xf3
int c = -3;
cout << a << " " << b << " " << c << " " << endl;
mystery(c, a, &b);
cout << a << " " << b << " " << c << " " << endl;
return 0;
}
Mystery Function: What prints out?
void mystery(int a, int& b, int* c) {
a++;
(*c)--;
b += *c;
Answer:
cout << a << " " << b << " " << *c << " " << endl;
} 4 8 -3
int main() { a b c
int a = 4;
int b = 8;
4 8 -3
0x12 0xab 0xf3
int c = -3;
cout << a << " " << b << " " << c << " " << endl;
mystery(c, a, &b);
cout << a << " " << b << " " << c << " " << endl;
return 0;
}
Mystery Function: What prints out?
a b c
void mystery(int a, int& b, int* c) {
a++; -3 /// 0xab
(*c)--; 0x5e ///// 0x7c
b += *c;
Answer:
cout << a << " " << b << " " << *c << " " << endl;
} 4 8 -3
int main() { a b c
int a = 4;
int b = 8;
4 8 -3
0x12 0xab 0xf3
int c = -3;
cout << a << " " << b << " " << c << " " << endl;
mystery(c, a, &b);
cout << a << " " << b << " " << c << " " << endl;
return 0;
}
Mystery Function: What prints out?
a b c
void mystery(int a, int& b, int* c) {
a++; -2 /// 0xab
(*c)--; 0x5e ///// 0x7c
b += *c;
Answer:
cout << a << " " << b << " " << *c << " " << endl;
} 4 8 -3
int main() { a b c
int a = 4;
int b = 8;
4 8 -3
0x12 0xab 0xf3
int c = -3;
cout << a << " " << b << " " << c << " " << endl;
mystery(c, a, &b);
cout << a << " " << b << " " << c << " " << endl;
return 0;
}
Mystery Function: What prints out?
a b c
void mystery(int a, int& b, int* c) {
a++; -2 /// 0xab
(*c)--; 0x5e ///// 0x7c
b += *c;
Answer:
cout << a << " " << b << " " << *c << " " << endl;
} 4 8 -3
int main() { a b c
int a = 4;
int b = 8;
4 7 -3
0x12 0xab 0xf3
int c = -3;
cout << a << " " << b << " " << c << " " << endl;
mystery(c, a, &b);
cout << a << " " << b << " " << c << " " << endl;
return 0;
}
Mystery Function: What prints out?
a b c
void mystery(int a, int& b, int* c) {
a++; -2 /// 0xab
(*c)--; 0x5e ///// 0x7c
b += *c;
Answer:
cout << a << " " << b << " " << *c << " " << endl;
} 4 8 -3
-2 11 7
int main() { a b c
int a = 4;
int b = 8;
11 7 -3
0x12 0xab 0xf3
int c = -3;
cout << a << " " << b << " " << c << " " << endl;
mystery(c, a, &b);
cout << a << " " << b << " " << c << " " << endl;
return 0;
}
Mystery Function: What prints out?
a b c
void mystery(int a, int& b, int* c) {
a++; -2 /// 0xab
(*c)--; 0x5e ///// 0x7c
b += *c;
Answer:
cout << a << " " << b << " " << *c << " " << endl;
} 4 8 -3
-2 11 7
int main() { a b c 11 7 -3
int a = 4;
int b = 8;
11 7 -3
0x12 0xab 0xf3
int c = -3;
cout << a << " " << b << " " << c << " " << endl;
mystery(c, a, &b);
cout << a << " " << b << " " << c << " " << endl;
return 0;
}
Calling Functions by Reference
Pointer arithmetic
Increment/decrement pointer (++ or --)
Add/subtract an integer to/from a pointer( + or += , - or -=)
Pointers may be subtracted from each other
Pointer arithmetic meaningless unless performed on pointer to
array
5 element int array on a machine using 4 byte ints
vPtr points to first element v[0], which is at location 3000
location
vPtr = 3000
3000 3004 3008 3012 3016
vPtr += 2; sets vPtr to 3008
v[0] v[1] v[2] v[3] v[4]
vPtr points to v[ 2 ]
Subtracting pointers
Returns number of elements between two addresses
vPtr2 = v[ 2 ];
vPtr = v[ 0 ];
vPtr2 - vPtr → 2
Pointer assignment
Pointer can be assigned to another pointer if both of same type
If not same type, cast operator must be used
Exception: pointer to void (type void *)
Generic pointer, represents any type
No casting needed to convert pointer to void pointer
void pointers cannot be dereferenced
Pointer Expressions & Arithmetic
Pointer comparison
Use equality and relational operators
Comparisons meaningless unless pointers point to members
of same array
Compare addresses stored in pointers
Example: could show that one pointer points to higher
numbered element of array than other pointer
Common use to determine whether pointer is 0 (does not
point to anything)