1

I built a function to generate a random 2-d array in C++. I was hoping that I could set the size of the array at compile time, so I included variables for the number of rows and columns in the array. However, when I try to compile the function, I get an error about the storage size of the array is not constant. This seems to do with the the static keyword that I have to add to the array definition, so that I can return the pointer from the function. I was not sure if there is a way around this error? Any suggestions.

double * generate_random_array(int rows, int cols, double lower_, double upper_){

static double test_array[rows][cols];

for (int i = 0; i < sizeof test_array / sizeof test_array[0]; i++) {
    for (int j = 0; j < sizeof test_array[0] / sizeof(double); j++) {
        test_array[i][j] = generate_random_numbers(lower_, upper_);
    }
}
return(test_array);

}
2
  • 1
    rows and cols are not known at compile time, you can call generate_random_array with whatever arguments you want. Commented Mar 10, 2017 at 19:00
  • Yeah, I was just generating the random array as part of some test functions. So I can change the row, column length in the test case, and then compile the test case. Sorry if I was unclear about that. Yeah, I am going to test with vectors as well, but thanks for the tip. Commented Mar 10, 2017 at 19:04

1 Answer 1

6

You can make generate_random_array a template, enforcing rows and cols to be known at compile-time:

template <int rows, int cols>
double* generate_random_array(double lower_, double upper_)
{
    /* ... as before ... */
}

Example usage:

generate_random_array<5, 10>(51.4, 66.0);

Nevertheless, you should use std::array instead of C-style arrays. If you want resizable arrays, then you should use std::vector instead.

std::array example:

template <int rows, int cols>
auto generate_random_array(double lower_, double upper_)
{
    const auto idx = [](int x, int y){ return y * rows + x; };
    std::array<double, rows * cols> result;

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            result[idx(i, j)] = generate_random_numbers(lower_, upper_);
        }
    }

    return result;
}

Example usage:

auto test_array = generate_random_array<5, 10>(11.0, 66.33);

live wandbox example

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

4 Comments

That is great. This should work just fine. I would not have thought of using a template, but I am still new to C++. Thanks for the tip about using std::array and std::vector in the future.
Say Vittorio, just had a question. I noticed in the answer to this post you actually create and return a std::array from the function by value. I thought that returning a C++ array by value was not possible--you could only return a pointer to the first element. Does std::array act like std::vector in that you can return those containers by value in functions?
@krishnab: yes, it does.
Oh great. Thanks for the tip. I did not know that.

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.