0

i am new to c++ and i am trying to create an Array from within a function with just the array size, [row][col], as the argument. I know you cannot return an array from a function in c++. I would allocate memory in these situations if i were to code in C. However, i am not sure what is the C++ way (:

I tried,

    int** get2Darray(int row, int col){
         int** randInts = new int[row][col]; //Invalid. Constant required.
         return randInts;
    }

Thanks.

EDIT: vector class does the trick, however, my program is heavily modular. I dont have access to the main program. My function should be called and a fresh array should be created and returned. Implementation should only be done inside this function

8
  • 1
    You should return std::vector<std::vector<int>> &. Commented Mar 4, 2016 at 18:56
  • Am i required to include any additional libraries? Commented Mar 4, 2016 at 18:58
  • yes, #include <vector> Commented Mar 4, 2016 at 19:00
  • when i return a vector type, would i able to use it on a int** type? Commented Mar 4, 2016 at 19:22
  • 1
    @user859385, you are returning an array of uninitialized pointers. Commented Mar 4, 2016 at 19:39

2 Answers 2

3

Either change int** randInts = new int[row][col]; into int* randInts = new int[row * col]; or define int** randInts as follows:

int** randInts = new int*[row];
for (int i = 0; i < row; ++i) {
    randInts[i] = new int[col];
}

It's a C way, not a C++ way. In C++ you should use containers (e.g. a vector as suggested in the comments).

Sign up to request clarification or add additional context in comments.

Comments

3

I have shown this using vector <vector<T> >, but you can also use Eigen matrix or boost::ublas::matrix

std::vector< std::vector<int> > foo(const int rows, const int cols)
{
    std::vector< std::vector<int> > v;
    v.resize(rows);
    for (auto i = 0; i < rows; i++)
    {
        v[i].resize(cols);
    }
    return v;
}

This will return a matrix of desired dimensions, and all elements will be initialized to 0.

The std::move part invokes a behavior called move semantics. You can read up on it. It's basically a better way of returning objects across a function boundary.

For compatibility's sake, if you must have a native array, then do this :

std::vector< vector<int> > temp = foo(rows, cols);
int *v = &temp[0][0];

Now, you can implement the gymnastics to get the correct index inside of int *v. You don't need multiple indirection.

P.S. This implementation assumes that your compiler supports C++ 11.

10 Comments

You should probably mention this is C++11.
@blazs agree. Thanks.
Shouldn't the return type of the function be non-void?
It knows to do Return Value Optimization. Scott Meyers explains it in this talk: youtube.com/watch?v=smqT9Io_bKo
@FirstJens See ideone.com/OqHHzG note how "without move" directly constructs into the return value not using an intermediate.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.