Pointers
Arrays
COMP315
2024
1
Pointers Summation
• Pointers are used to store and manage the addresses of dynamically
allocated blocks of memory. Such blocks are used to store data
objects or arrays of objects. Most structured and object-oriented
languages provide an area of memory, called the heap or free store,
from which objects are dynamically allocated.
• Pointers have a data type just like variables, for example an integer
type pointer can hold the address of an integer variable and a
character type pointer can hold the address of a char variable.
2
Advantages of Pointers
1. It allows management of structures which are allocated memory
dynamically
2. It allows passing of arrays and strings to functions more efficiently
3. It allows the possibility to pass address of structure instead of entire
structure to the functions
• Some find it easier to deal with references rather than pointers, but in
the end there is really only a syntactic difference (neither of them
pass by value)
3
Arrays and Pointers
• Static arrays are allocated memory at compile time and
the memory is allocated on the stack
• Dynamic arrays are allocated memory at the runtime and
the memory is allocated from heap
4
Arrays in Memory
int [] a = {4, 7, 11};
a[0] a[1] a[2]
a=
5
Arrays in Memory
int [] a = {4, 7, 11};
a[0] a[1] a[2]
a=
In this scenario, assume that each int is 2 bits, and hence,
takes up 2 memory locations 6
Arrays in Memory
int [] a = {4, 7, 11};
a[0] a[1] a[2] 4
a= 7
11
7
a
a
8
9
Array Operations
int [] a = {4, 7, 11};
cout << a; // displays 1023
cout << a[0]; // displays 4
cout << *a; // displays 4
cout << &a; // displays 1023
cout << &a[0]; // displays 1023
cout << &a[1]; // displays 1025
cout << &a[2]; // displays 1027
int *aPointer = a;
cout << aPointer; // displays 1023
cout << *aPointer; // displays 4
cout << &aPointer; // displays 1029
cout << aPointer[0]; // displays 4
cout << aPointer[1]; // displays 7
10
Array Access
int a[] = {4, 7, 11};
int *aPointer = a;
cout << a[0]; // displays 4
cout << aPointer[0]; // displays 4
cout << (a+1) // displays 1025
cout << (aPointer+1); // displays 1025
cout << *(a+1); // displays 7
cout << *(aPointer+1); // displays 7
11
Strings
• Strings are an array of characters
• C-Strings are not like values and variables of other data types, and
many of the usual operations do not work for C-Strings.
• Assignments of strings cannot use the =
• Equality of strings cannot be checked using the ==
• NOTE: Initialisations are not the same as assignments
• There are predefined functions for strings, and this is a C++ standard
library
12
13
Copying Strings char* strcpy(char* dest, const char* src);
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
char src[] = “Toni Kroos!”;
char dest[20]; // Large enough to store content of src
strcpy(dest,src);
cout << dest; // Displays: Toni Kroos!
return 0;
} 14
The Null character, ‘\0’
• The null character, ‘\0’ is used to mark the end of a C-String that is
stored in an array of characters
15
16
Passing Classes as Parameters
• If a class is passed by reference, the copy constructor will be used to
make a copy
• This is computationally expensive
bool IsImageGreen(Image img);
17
Passing Classes as Parameters
• It is much faster to pass by reference:
bool IsImageGreen(Image *img);
or
bool IsImageGreen(Image &img);
18
Copy Constructor
• A copy constructor is a constructor that has one call-by-reference
parameter that is the same as the class
• The copy constructor for a class is called automatically when:
▪ a function returns a value of the class type
▪ an argument is plugged in for a call-by-value parameter of the class type
• Copy constructors can be used in the same ways as other
constructors
• Any class that uses pointers and the new operator, should have a
copy constructor
19
Copy Constructor
• A copy constructor is called when a new object is created from an
existing object, as a copy of the existing object
• The assignment operator is called when an already initialized object is
assigned a new value from another existing object
20
#include<iostream>
using namespace std;
class Point
{
private:
int x, y;
public:
Point(int x1, int y1) { x = x1; y = y1; }
Point(const Point &p2) {x = p2.x; y = p2.y; } // Copy constructor
int getX() { return x; }
int getY() { return y; }
};
int main()
{
Point p1(10, 15); // Normal constructor is called here
Point p2 = p1; // Copy constructor is called here
cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY(); // p1.x = 10, p1.y = 15
cout << "\np2.x = " << p2.getX() << ", p2.y = " << p2.getY(); // p1.x = 10, p1.y = 15
return 0;
21
}
22
23
24
25
.h and .cpp
26
Circular Dependencies of #includes
27
Forward Declarations
28
new arrays
int x = 10;
int* nums1 = new int[10];
int* nums2 = new int[x];
• Initializes an array of 10 integers on the heap.
29
new multidimensional arrays
int x = 3, y = 4;
int** nums3 = new int[x][4];
int** nums4 = new int[x][y];// BAD!
• Initializes a multidimensional array
• Only the first dimension can be a variable. The rest must be
constants.
30
delete arrays
// allocate memory
int* nums1 = new int[10];
int* nums3 = new int[x][4][5];
...
// free the memory
delete[] nums1;
delete[] nums3;
• Have to use delete[]
31
32
33
34