简体   繁体   English

初始化指向指针的向量

[英]Initialize vector of pointers to pointers

I have troubles initializing a vector of pointers to pointers (which I shouldn't do anyways I guess but please forgive me). 我在初始化指向指针的向量时遇到麻烦(我猜我不应该这样做,但是请原谅我)。 I basically have a pointer z to a variable containing 0 and now I want to initialize a matrix that contains pointers to that pointer. 我基本上有一个指向包含0的变量的指针z ,现在我想初始化一个包含指向该指针的指针的矩阵。

void example(int nrow, int ncol) {
    int zero = 0;
    int* z = &zero;
    int size = nrow * ncol;
    vector<int **> pointerMatrix(size);
    for (int i = 0; i < size; i++) {
        int** a = &z;       
        pointerMatrix.push_back(a);
    }
    //following line throws an exception:
    cout << **pointerMatrix[0] << endl;
}

Now the above code throws an exception, apparently there is nothing to be found under the address the pointer points to. 现在上面的代码引发了异常,显然在指针指向的地址下找不到任何内容。

Anyone sees my mistake? 有人看到我的错误吗?

As described in the documentation pointerMatrix(size); 文档中所描述的pointerMatrix(size); creates a vector with having size amount of default-initialized ( nullptr ) elements, hence after the loop, your vector contains 2*size elements, having the first half filed in as nullptr , and the second half filed in as actual elements, hence cout << **pointerMatrix[0] << endl; 创建一个向量,该向量的size为默认初始化的( nullptr )元素,因此在循环之后,您的向量包含2*size元素,其中前半部分为nullptr ,后半部分为实际元素,因此cout << **pointerMatrix[0] << endl; tries to dereference a nullptr . 尝试取消引用nullptr

To fix this, you can, either, call reserve , to avoid unnecessary reallocations: 要解决此问题,您可以致电reserve ,以避免不必要的重新分配:

vector<int **> pointerMatrix;
pointerMatrix.reserve (size);

Or use operator[] to assign the values, instead of pushing them: 或使用operator[]分配值,而不是推送它们:

for (int i = 0; i < size; i++) {
    int** a = &z;       
    pointerMatrix[i] = a; // Or pointerMatrix[i] = &z;
}

push_back is used to add a new element past the end of the vector. push_back用于在向量的末尾添加新元素。 It effectively increases the size of the vector to add the element at the end. 它有效地增加了向量的大小,以在最后添加元素。

You already specified the size of your vector, so you can just access (and modify) the elements with operator[] for example: 您已经指定了向量的大小,因此您可以仅使用operator[]访问(并修改)元素,例如:

for (int i = 0; i < size; i++) {
    pointerMatrix[i] = &z;
}

Alternatively, use a range-based for loop: 或者,使用基于范围的for循环:

for (auto& entry : pointerMatrix) { // auto& can be replaced with int**&
    entry = &z;
}

You allocate an array with the desired size, and then still use push_back to add further elements at the end. 您分配具有所需大小的数组,然后仍然使用push_back在末尾添加其他元素。

Since you intend to fill the whole array with the same element, you should use this: 由于您打算用相同的元素填充整个数组,因此应使用以下命令:

vector<int **> pointerMatrix(size, &z);
vector<int **> pointerMatrix(size);

should be like 应该像

vector<int **> pointerMatrix;

Seems like there is a typo mistake in your implementation as pointer matrix seems already a size before the loop. 似乎您的实现中有错别字,因为指针矩阵似乎已经在循环之前确定了大小。 you should either push a pointer value in index or dynamically push this. 您应该在索引中推送指针值或动态推送该值。 For this i guess, declaring vector pointerMatrix without defining size should works. 为此,我猜想,在不定义大小的情况下声明向量pointerMatrix应该有效。

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

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