user2153561
user2153561

Reputation: 25

C++, Passing a 2D array on stack to function by reference

void build() will create a 2D array whose size is determined at run time and pass it to modify() by reference.

void modify() will make some change of the array.

.h file:

void modify(______, int mySize);
void build(int size);

in .cpp file:

void modify(______, int mySize) {
    for (int i = 0; i < mySize; i++) 
        for (int j = 0; j < mySize; j++)
            myArray[i][j] = false;
}

void build(int size) {
    bool myArray[size][size];
    modify(______, size);
}

Can someone tell me what to put in these blanks? I tried many way to cast myArray but still not working. Thank you so much!

Upvotes: 1

Views: 1720

Answers (2)

Joseph Mansfield
Joseph Mansfield

Reputation: 110728

First, note that variable length arrays (VLAs) are not standard C++. The fact that this line compiles is due to a GCC compiler extension:

bool myArray[size][size];

It simply isn't valid C++. The dimensions of your array need to be compile time constants, yet here you're using arguments passed to your function build.

Anyway, to pass a VLA to a function, you have to continue using compiler extensions. However, as far as I've tested, these only work when compiling as C. The first option uses an extension that allows parameters to be used in other parameter declarations:

void modify(int mySize, bool array[][mySize]);

The second option, if you want mySize to be the second argument, also uses a compiler extension allowing forward declarations of parameters:

void modify(int mySize; bool array[][mySize], int mySize);

Here int mySize; is a parameter forward declaration.

Nonetheless, you really shouldn't be using variable length arrays. You can dynamically allocate arrays of variable length, like so:

void modify(bool** array, int mySize);

void build(int size) {
  bool** myArray = new bool*[size];
  for (int i = 0; i < size; i++) {
    myArray[i] = new bool[size];
  }
  modify(myArray, size);
}

However, this is still not a recommended practice in C++. You should avoid dynamic allocation unless you really need it, and when you do need it you should manage it in some way. Here, you would need to remember to delete[] each element of myArray and myArray itself.

What you should really be using is the standard containers. A std::vector<std::vector<bool>> would suit you well here:

void modify(std::vector<std::vector<bool>>& array);

void build(int size) {
  std::vector<std::vector<bool>> myArray(size, std::vector<bool>(size));
  modify(myArray);
}

Now you don't even have to pass along the size.

Upvotes: 1

Zoltan Tirinda
Zoltan Tirinda

Reputation: 799

Use it this way:

    void modify(bool**& myArray, const int mySize) 
    {
        for (int i = 0; i < mySize; i++) 
            for (int j = 0; j < mySize; j++)
               myArray[i][j] = false;
    }

    void build(const int size) 
    {
        // create the array
        bool** myArray = new bool*[size];
        for (int i=0; i<size; i++)
            myArray[i] = new bool[size];

        modify(myArray, size);

        // release the array
        for (int i=0; i<size; i++)
            delete[] myArray[i];
        delete[] myArray; 
    }

Upvotes: 0

Related Questions