Created
September 17, 2022 10:45
-
-
Save Vaneeza-7/528f0e0976c2041d63d2089990003fef to your computer and use it in GitHub Desktop.
Dynamically Allocated Arrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//1D array | |
void allocateOneD(int*& p, int size) | |
{ | |
p = new int* [size]; | |
} | |
void deallocateOneD(int* p, int size) | |
{ | |
delete[] p; | |
} | |
//2D array | |
void allocateTwoD(int**& p, int nrows, int ncols) | |
{ | |
p = new int* [nrows]; | |
for (int i = 0; i < nrows; i++) | |
{ | |
p[i] = new int[ncols]; | |
} | |
} | |
void deallocateTwoD(int** p, int nrows, int ncols) | |
{ | |
for (int i = 0; i < nrows; i++) | |
{ | |
delete[] p[i]; | |
} | |
delete[] p; | |
} | |
//3Darray | |
void allocateThreeD(int***& p, int npages, int rows, int cols) | |
{ | |
p = new int** [npages]; | |
for (int i = 0; i < npages; i++) | |
{ | |
p[i] = new int* [rows]; | |
for (int j = 0; j < rows; j++) | |
{ | |
p[i][j] = new int[cols]; | |
} | |
} | |
} | |
void deAllocateThreeD(int*** p, int npages, int rows, int cols) | |
{ | |
for (int i = 0; i < npages; i++) | |
{ | |
for (int j = 0; j < rows; j++) | |
{ | |
delete[] p[i][j]; | |
} | |
delete[] p[i]; | |
} | |
delete[] p; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment