0% found this document useful (0 votes)
3 views

Dynamic 2d Array

The document discusses dynamic arrays, focusing on pointer arithmetic and the creation of two-dimensional dynamic arrays. It explains how to perform arithmetic operations on pointers and how to allocate and initialize a 2D dynamic array using pointers. Additionally, it covers the proper method for deleting a multidimensional dynamic array to prevent memory leaks.

Uploaded by

lyujiayi7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Dynamic 2d Array

The document discusses dynamic arrays, focusing on pointer arithmetic and the creation of two-dimensional dynamic arrays. It explains how to perform arithmetic operations on pointers and how to allocate and initialize a 2D dynamic array using pointers. Additionally, it covers the proper method for deleting a multidimensional dynamic array to prevent memory leaks.

Uploaded by

lyujiayi7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CMPT130 J.

Ye

Dynamic Arrays

• Pointer arithmetic
• Two-dimensional dynamic array
Pointer Arithmetic
• Arithmetic (addition and subtraction) can be
performed on the addresses contained in pointers
– Using the dynamic array of doubles declared previously
(Dynamic 1-d array slide #7), recall that pt points to pt[0]
– The expression pt+1 evaluates to the address of
pt[1] and pt+2 evaluates to the address of pt[2]
• Notice that adding one adds enough bytes – the size of
the base type (in this example, double)

2
Pointer Arithmetic Operations
• You can add (+) and subtract (-)with pointers
– The ++ and -- operators can also be used
– Two pointers of the same type can be subtracted to
obtain the number of indexed variables between them
• The pointers should be in the same array

– This code shows one way to use pointer arithmetic:


for (int i = 0; i < array_size; i++)
cout << *(pt + i) << " " ;
// same as cout << pt[i] << " "

3
Allocating Memory for 2-d Dynamic Arrays
• To create a 4x3 (4 rows x 3 columns) two-dimensional
dynamic array
– View the 2-d array as an array of arrays
– First create a one-dimensional dynamic array of pointers
• Start with a new definition:
typedef int* IntArrayPtr;
• Now create a dynamic array of pointers named matrix:
IntArrayPtr* matrix = new IntArrayPtr[4];
//The above two statements can be combined to: int** matrix = new int*[4];
– For each pointer in matrix, create a dynamic array of
integers:
for (int i = 0; i < 4; i++)
matrix[i] = new int[3];
4
• The dynamic array created on the previous slide
could be visualized like this:

matrix These boxes are


int* type
This box is
int** type

These
boxes are
int type

• Now, how to initialize this 2-d array (matrix)?

5
Deleting the 2-d Dynamic Array
• To delete a multidimensional dynamic array
– Each call to new that created an array must have a
corresponding call to delete[]
– Example:
To delete the dynamic array shown on the previous slide:

for(int i = 0; i < 4; i++)


delete [] matrix[i]; //delete the arrays of 3 int's
delete [] matrix; // delete the array of IntArrayPtr's

You might also like