0

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!

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • I think my title is a bit unclear. What inspired me was this post https://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new I want to use new to allocate memory on the heap. The only thing different between their example, which is this ' int** a = new int*[rowCount]; 'for(int i = 0; i < rowCount; ++i)' a[i] = new int[colCount]; Is that I want the colCount to be determined when the program runs. – Megan Vineyard Aug 29 '19 at 04:04

2 Answers2

0

Why don't you use vector<vector<int>>? Vector is a dynamic array.

David G
  • 94,763
  • 41
  • 167
  • 253
0

I have no idea what the line *(arr + index) = (new int + index); even supposed to do.

If you want to allocate array of ints size columnSize you write it as:

int * column = new int[columnSize]

If you want to store the pointer in arr at location index then write

arr[index] = new int[columnSize]

I am not sure what you wanted to write, your code is confusing for me. Why don't you use std::vector<int>? Or std::vector<std::vector<int> >?

ALX23z
  • 4,456
  • 1
  • 11
  • 18
  • 1
    `(new int + index)` is get me a pointer to a single integer, then advance the pointer `index` integers after it. A nearly perfect Undefined Behaviour generator. – user4581301 Aug 29 '19 at 02:55
  • I thought what I was doing with new was returning a pointer to memory on the heap to arr. The next time it ran it would increment by 1 to the adjacent memory location. It looks like that is what is happening when I print out the addresses... – Megan Vineyard Aug 29 '19 at 04:23