I am trying to create a two dimensional dynamically allocated array who's column size is increased every time the user wants to enter an additional number in the array. That is, a new address will be assigned on the heap and returned to "arr" In my example the rows are constant. The problem is I can't dynamically allocate memory and assign integers to my array to any other row besides the first.
int allocate(int** &arr, char choice)
{
int x = 1;
int index = 0;
int row = 0;
int colCount = 0;
do
{
*(arr + index) = (new int + index);
arr[row][index] = x;
//(arr[0]+index)= new int*[index]; this fundementally does not work, cant modify left value
colCount++;
cout << x << "'s address " << &arr[row][index] << " I have " << colCount
<< " columns " << endl;
x++;
index++;
cout << "Select another number?" << endl;
cin >> choice;
} while (choice != 'n');
return colCount;
}
int main()
{
int rowCount = 3;
int colCount = 0;
int **arr = new int*[rowCount];
char choice = 'n';
colCount = allocate(arr, choice);
for (int i = 0; i < colCount; i++)
{
delete[] arr[i];
}
delete[] arr;
return 0;
}
My problem is with this line of code here
*(arr + index) = (new int + index);
while it does print out the values and addresses I allocated assigned in my function, I get a heap corruption when I try to delete the memory I assigned .Also, I can't figure out how to get the numbers to assign
Also, if I am not mistaken *(arr + index)
is giving me pointers of the first column only! So I am not even sure why this is working!