You are allocing array of pointers wrong.
First you have to allocate enough space for row
pointers
int** pptr = new int* [rows];
For every pointer enough space for col
integers
for (int i = 0; i < cols; i++)
{
pptr[i] = new int [cols];
}
To delete arrays use delete[]
instead of delete
.
Delete every individual row
for (int i = 0; i < rows; i++)
{
delete [] pptr[i];
}
Then delte array of pointers
delete [] pptr;
There is no need to assigning NULL
to deleted pointer since you wont use them again. Also in c++ you should use nullptr
instead of NULL
.
Here is the correct using of array of pointers.
Your mistakes
int* ptr = new int [rows];
int** pptr = new int* [cols];
*pptr=ptr;
- Swapped rows & cols
- Allocated memory only for first pointer/row, others were uninitialized -> UB
- Used
delete
instead ofdelete[]
solved Finding out character ‘a’ from a file then count the lines in which the character is appeared [duplicate]