views:

83

answers:

2

I have allocated an array as follows.

#include <iostream>

int main() {
    const int first_dim = 3;
    const int second_dim = 2;

    // Allocate array and populate with dummy data
    int** myArray = new int*[first_dim];
    for (int i = 0; i < first_dim; i++) {
        myArray[i] = new int[second_dim];
        for (int j = 0; j < second_dim; j++) {
            myArray[i][j] = i*second_dim + j;
            std::cout << "[i = " << i << ", j = " << j << "] Value: " << myArray[i][j] << "\n";
        }
    }

    // De-allocate array
    for (int i = 0; i < first_dim; i++)
        delete[] myArray[i];
    delete[] myArray;
}

Let's say I want to add a 4th element to the first dimension, i.e. myArray[3]. Is this possible?

I've heard that Vectors are so much more efficient for this purpose, but I hardly know what they are and I've never used them before.

+5  A: 
Armen Tsirunyan
Just a pedantic point: can't you just reallocate `myArray` to size 4 and assign the pointers? You can keep the same sub arrays and just point to them from the reallocated `myArray`, right?
JoshD
JoshD: Don't sabotage my tries to scare people away from arrays :) When you tell your children not to do drugs you don't mention that if you smoke pot a couple of times that won't make you a terrorist, though it is true :)
Armen Tsirunyan
You can do better than that: `vector<vector<int> > matrix(3, vector<int>(2));` Creates and initializes the matrix in one line.
Martin York
@Martin :) I know, I just wanted to keep analogy with what the OP wrote. I think he would have much difficulty understanding vector<vector<int> > matrix(3, vector<int>(2)); And anyway after the resize we would have to resize matrix[3]. Please keep in mind that the OP has no experience with vectors. This example is meant to encourage him to study more. And once he does, he will learn details.
Armen Tsirunyan
@Martin I am at least glad you didn't suggest using iterators instead of indexing :)
Armen Tsirunyan
Armen I could not understand the last item about deleting array but not a pointer. Is "new int[second_dim]" an array or a pointer? It is a dinamically allocated array. But when you delete it, you write "delete[] myArray[i]". I could not understand what is your remark at this point.
Narek
@Narek. My point was that both `new int` and `new int[10]` return a pointer to int (`int*`). In order to free the memory you should explicitly tell the compiler while deleting how to treat that pointer - as one that points to one int or at one that points to an array,
Armen Tsirunyan
@Narek: Sorry in the original OP's code, which he has edited he wrote `delete array[i];` and `delete array;` which is undefined behaviour in C++
Armen Tsirunyan
Ok, I got it, thanks.
Narek
If I declare a vector as a class member, do I have to manually free the memory that was used by the vector in the class destructor?
Pieter
No, as if the vector is a member (and not a pointer to the vector which is dynamically allocated later) than during your class destructor work all member destructors are being called, and of course, the verctors detructor is being called too.
Narek
+1  A: 

Let's say I want to add a 4th element to the first dimension, i.e. myArray[3]. Is this possible?

Yes, but it's a pain in the neck. It basically boils down to allocating a new array, just as your existing code does (hint: put it in the function and make the sizes arguments to that function) and copying compatible elements over.

Edit: One of the things that std::vector does for you is properly de-allocating you memory. In the code you have, failure to allocate one of the arrays along the 2nd dimension will result in a memory leak. A more robust solution would initialize pointers to 0 before performing any allocation. An exception block could then catch the exception and free whatever was partially allocated.

Because this code becomes complex quickly, people resort to allocating a single buffer and addressing using a stride or using a 1D array of 1D arrrays (i.e. std::vector of std::vectors).

André Caron