简体   繁体   English

C ++,将堆栈上的2D数组传递给引用以起作用

[英]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 build()将创建一个2D数组,其大小在运行时确定,并将其通过引用传递给modify()

void modify() will make some change of the array. void modify()将对数组进行一些更改。

.h file: .h文件:

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

in .cpp file: 在.cpp文件中:

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. 我尝试了多种方法来投射myArray但仍然无法正常工作。 Thank you so much! 非常感谢!

First, note that variable length arrays (VLAs) are not standard C++. 首先,请注意可变长度数组(VLA)不是标准的C ++。 The fact that this line compiles is due to a GCC compiler extension: 该行编译的事实归因于GCC编译器扩展:

bool myArray[size][size];

It simply isn't valid C++. 它根本不是有效的C ++。 The dimensions of your array need to be compile time constants, yet here you're using arguments passed to your function build . 数组的维数必须是编译时间常数,但是这里您使用的是传递给函数build参数。

Anyway, to pass a VLA to a function, you have to continue using compiler extensions. 无论如何,要将VLA传递给函数,您必须继续使用编译器扩展。 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: 但是,就我所测试的而言,它们仅在编译为C时才起作用。第一个选项使用扩展名,该扩展名允许在其他参数声明中使用参数:

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: 第二个选项,如果希望mySize作为第二个参数,则还使用编译器扩展,允许向前声明参数:

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

Here int mySize; 这是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++. 但是,这仍然不是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. 在这里,您需要记住要delete[] myArray myArray本身的每个元素。

What you should really be using is the standard containers. 您真正应该使用的是标准容器。 A std::vector<std::vector<bool>> would suit you well here: 一个std::vector<std::vector<bool>>在这里很适合您:

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. 现在,您甚至不必传递大小。

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; 
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM