Review on pointers and dynamic objects Memory Management

Review on pointers and dynamic objects

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]? ? ? } }

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

Why pointers? Dynamic objects are ‘implemented’ or ‘realized’ by ‘pointers’ which are parts of low-level ‘physical memory’ We don’t like it, but can not avoid it. Low level languages directly manipulate them High level languages want to ‘hide’ the pointers (conceptually remove them)

Pointers § A pointer is a variable used for storing the address of a memory cell. § We can use the pointer to reference this memory cell Memory address: 1020 … … int a; int* p; 1024 100 … 10032 1024 … a Integer a Pointer p

Getting an address: address operator & int a=100; “&a” “the address of a” Memory address: 1020 … … 1024 100 a int a = 100; cout << a; Cout << &a; 100 1024 … … …

Dereferencing Operator * § We can access to the value stored in the variable pointed to by preceding the pointer with the “star” operator (*), Memory address: … 1020 88 1024 10032 … a int a = 100; int* p = &a; cout << a << endl; cout << &a << endl; cout << p << " " << *p << endl; cout << &p << endl; 1024 … p *p gives 100

Pointer to pointer … int a; int* p; int** q; a = 58; p = &a; q = &p; 58 a p q a, *p, and **q are the same object whose value is 58! But q = &a is illegal!

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 << *ptr << j << endl; // display "12"

Writing pointer type properly in C++ … int* ? a; int* b; int *a, *b; int* a, b are both integer pointers a is integer pointer, b is just integer! I don’t like this! a, b; typedefine int My. Int; My. Int k; typedefine int* Int. Pt; Int. Pt a, b; int k; Recommended!!!

Summary * has two usages: - pointer type definition: - dereferencing: int a; int* p; *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; ?

Pointers and References Reference (implemented as a (const) pointer) is an abstraction, Not available in C, only in C++.

Pointer vs. Reference § A pointer can be assigned a new value to point at a different object, but a reference 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 << “value of m = “ << m <<endl; //value of m printed is 12 n = 36; Cout << “value of j = “ << j << endl; //value of j printed is 12 p = &n;

§ A reference variable is different from a pointer int x=10; int* ref; Ref = &x; x ref 10 int x=10; int& ref = x; x ref 10

Traditional Pointer Usage void swap(char* ptr 1, char* ptr 2){ char temp = *ptr 1; *ptr 1 = *ptr 2; *ptr 2 = temp; } int main() { char a = 'y'; char b = 'n'; swap(&a, &b); cout << a << b << endl; return 0; } Uese pass-by-value of pointers to ‘change’ variable values C language does not have ‘call by reference’!

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 << a << b << endl; return 0; } y, z are ‘references’, only names, not like ptr 1, ptr 2 that are variables
![Pointers and Arrays Double faces of an array: int a[10] l a is the Pointers and Arrays Double faces of an array: int a[10] l a is the](http://slidetodoc.com/presentation_image_h2/6a66ac1f7475cf30ce0185c576678f77/image-17.jpg)
Pointers and Arrays Double faces of an array: int a[10] l a is the name of an array, l a is also is a constant pointer to its first element

Pointers and Arrays The name of an array points only to the first element not the whole array. a[0] 2 a[1] 4 a[2] 6 a[3] 8 a[4] 22 a
![Dereference of an array name a[0] 2 a[1] 4 a[2] 6 a[3] 8 a[4] Dereference of an array name a[0] 2 a[1] 4 a[2] 6 a[3] 8 a[4]](http://slidetodoc.com/presentation_image_h2/6a66ac1f7475cf30ce0185c576678f77/image-19.jpg)
Dereference of an array name a[0] 2 a[1] 4 a[2] 6 a[3] 8 a[4] 22 #include <iostream> Using namespace std; void main(){ int a[5] = {2, 4, 6, 8, 22}; cout << *a << " " << a[0] << " " << *(&a[0]); . . . " } //main This element is called a[0] or *a Result is: 2 2 2

Array name as pointer To access an array, any pointer to the first element can be used instead of the name of the array. a p a[0] a[1] a[2] a[3] a[4] 2 4 6 8 22 a We could replace *p by *a #include <iostream> Using namespace std; void main(){ int a[5] = {2, 4, 6, 8, 22}; int* p = a; int i = 0; 2 cout << a[i] << " " << *p; 2. . . }

dynamic objects

Summary Static variables (objects) Dynamic variables (objects) A (direct) named memory location A static part (pointer) + (indirect) nameless memory location (dynamic part) int a; int* pa; a = 20; pa = new int; *pa = 20; a 20 static 20 pa static dynamic

Dynamic array Simple dynamic variable int* p = new int; *p = 10; delete p; p 10 ‘delete’ two actions: 1. Return the object pointed to 2. Point the pointer p to NULL int* p = new int[100]; for (i=1; i<100; i++) p[i] = 10; delete[] p; p 10 10 10 ‘delete p’ is not sufficient for an array!!!
- Slides: 23