Fancypants753
Fancypants753

Reputation: 449

Dynamic 2D arrays in C++ as parameters

I have a 2D array of doubles for a problem using dynamic programming. What I want to do is below (f is any function operating on the variable "size" and returns an int) :

void myFunction(int size){
    double myArr[size][f(size)];
    helperFunction(size, myArr);
}

void helperFunction(int size, double[][] myArr){
    //do something
}

However this code is obviously a mashup of java and c++. How would I accomplish this in c++?

Upvotes: 0

Views: 103

Answers (1)

dstackflow
dstackflow

Reputation: 77

Highly recommend using vectors as alternative to dynamic arrays. Vectors have a bad reputation in java, but in c++ they're the way to go. Demonstrating "a" c++ way of accomplishing this, using both "new" allocation and vectors. Warning: unlike java, primitive arrays in c++ will require you to keep track of array sizes; all inner arrays as well.

#include <iostream>
#include <vector>

class Test{
private:
    double **myArray;//Primitive 2d array; suggest using 2d vector.
    std::vector<std::vector<double>> myVector;//Alternative vector.
    std::vector<int> col;//Using vector for myArray sizes; where sizes = inner array size.
    int size;//Required with primitive dynamic array; size of array.
public:
    Test(){
        myArray = 0;//c++ initialization before reading; safety.
        size = 0;//c++ "" "" ""
    }
    ~Test(){
        flushArray();//Flush all data on destructor.
    }
    void flushArray(){//Empties 2d array and set it to 0.
        //Flush of primitive 2d array.
        for(int i = 0; i < size; ++i) {//Needs to delete every new data created
            delete []myArray[i];//Freeing memory for inner node.
        }
        delete []myArray;//Freeing memory for outer node.
        myArray = 0;//Setting pointer to 0;
        size = 0;//Setting size to 0;
        col.clear();//Flush for column vector: easy.

        //vector is self maintained and will clear itself on destructor; exception: "new" DATA.
    }
    void myFunction(int size) {
        if(this->size != 0) {//If there is already data...
            flushArray();//Flush Array;
        }
        this->size = size;//Require size to free during next call.
        myArray = new double*[size];//Create new array of nothings with a size of "size".
        for(int i = 0; i < size; ++i) {
            //Traversing through array and adding an array of doubles.
            myArray[i] = new double[f(size,false)];//New DATA can be implicit
        }
    }
    void otherFunction(int size) {
        myVector.clear();//Flush Vector;
        myVector.resize(size);//Automated dynamic sizing
        for(auto it = myVector.begin(); it != myVector.end(); ++it) {
            it->resize(f(size,true));
        }
    }
    int f(int size, bool isVector) {
        //.., do something.
        if(isVector) {
            return size;//Whatever int you were meant to return.
        }
        //Keep track of cols, maybe they'll vary
        col.push_back(size);//it might be (size+i)... Required for dynamic array.
        return col.back();//Return the intended size.
    }
    void printArraySize() {
        for(int i = 0; i < size; ++i) {
            std::cout<<"myArray["<<i<<"] has "<<col[i]<<" elements."<<std::endl;
        }
    }
    void printVectorSize() {
        //Using a counter, chose to use primitive for-loop.
        for(int i = 0; i < myVector.size(); ++i) {
            std::cout<<"myVector["<<i<<"] has "<<myVector[i].size()<<" elements. "<<std::endl;
        }
    }
};
int main()
{
    Test test;

    test.myFuntion(10);
    test.otherFunction(10);

    test.printArraySize();
    test.printVectorSize();

    return 0;
}

Printing results to show size:

myArray[0] has 10 elements.  
myArray[1] has 10 elements.  
myArray[2] has 10 elements.  
myArray[3] has 10 elements.  
myArray[4] has 10 elements.  
myArray[5] has 10 elements.  
myArray[6] has 10 elements.  
myArray[7] has 10 elements.  
myArray[8] has 10 elements.  
myArray[9] has 10 elements.  
myVector[0] has 10 elements.  
myVector[1] has 10 elements.  
myVector[2] has 10 elements.  
myVector[3] has 10 elements.  
myVector[4] has 10 elements.  
myVector[5] has 10 elements.  
myVector[6] has 10 elements.  
myVector[7] has 10 elements.  
myVector[8] has 10 elements.  
myVector[9] has 10 elements.  

Process returned 0 (0x0)   execution time : 0.011 s  
Press any key to continue.  

Basically with vectors: your data, their sizes, and lifespan are managed for you; exception vector of pointers to "new" data. Whereas in a primitive dynamic arrays, you'll have to keep track of your arrays sizes (all of them) and delete your data/arrays when done with.

Edit: Minor spelling check.

Upvotes: 1

Related Questions