将指针解引用为向量的奇怪输出 [英] Strange output from dereferencing pointers into a vector
问题描述
我正在用 C++ 编写一个程序,其中我需要一个二维指针网格,这些指针指向存储在向量中的对象.我测试了程序的某些部分,并在输出中看到了奇怪的结果.
I was writing a program in C++ where I need to have a 2d grid of pointers which point to objects which are stored in a vector. I tested out some part of the program and saw strange results in the output.
我将对象更改为整数并删除了所有不必要的内容以将其缩减为下面的代码片段,但我仍然得到一个奇怪的输出.
I changed the objects to integers and removed everything non-essential to cut it down to the code snippet below, but I still get a weird output.
vector<vector<int*>> lattice(10, vector<int*>(10));//grid of pointers
vector<int> relevant;//vector carrying actual values onto which pointers will point
for(int i = 0; i<10; i++){
int new_integer = i;
relevant.push_back(new_integer);//insert integer into vector
lattice[0][i] = &relevant[i];//let pointer point onto this value
}
//OUTPUT
for(int j = 0; j<10; j++){
cout<<*lattice[0][j]<<" ";
cout<<relevant[j]<<endl;
}
我得到如下奇怪的输出:
I get strange outputs like this:
19349144 0
19374040 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
此外,我的输出在每次运行时都会发生变化,这取决于我制作网格的大小.
Also, my output changes from run to run and depending on how big/small I make my grid.
我希望左边和右边的所有值都相等,我想我还没有理解关于指针的一些基本知识,如果这是一个非常基本的问题,很抱歉.
I would expect all the values on the left and right to be equal, I guess there is something fundamental about pointers I haven't understood, so sorry if this is a very basic question.
有人能解释一下为什么我会得到一些网格值的奇怪输出吗?
Can someone please explain why I get strange outputs for some values of the grid?
推荐答案
当一个 std::vector
插入时,如果它的新 size()
将超过它的当前 capacity()
,向量必须重新分配其内部数组以腾出空间,这将使任何现有的迭代器和指向旧内存的指针无效.
When a std::vector
is inserted into, if its new size()
will exceed its current capacity()
, the vector has to reallocate its internal array to make room, which will invalidate any existing iterators and pointers to the old memory.
在您的示例中,您可以通过提前 reserve()
'ing capacity()
来避免这种情况,例如:
In your example, you can avoid that by reserve()
'ing the capacity()
ahead of time, eg:
vector<vector<int*>> lattice(10, vector<int*>(10));//grid of pointers
vector<int> relevant;//vector carrying actual values onto which pointers will point
//ADD THIS!
relevant.reserve(10);//pre-allocate the capacity
for(int i = 0; i<10; i++){
int new_integer = i;
relevant.push_back(new_integer);//insert integer into vector
lattice[0][i] = &relevant[i];//let pointer point onto this value
}
//OUTPUT
for(int j = 0; j<10; j++){
cout<<*lattice[0][j]<<" ";
cout<<relevant[j]<<endl;
}
或者,您可以预先分配size()
,然后使用operator[]
而不是push_back()
,例如:
Alternatively, you can pre-allocate the size()
and then use operator[]
instead of push_back()
, eg:
vector<vector<int*>> lattice(10, vector<int*>(10));//grid of pointers
vector<int> relevant(10);//vector carrying actual values onto which pointers will point
for(int i = 0; i<10; i++){
int new_integer = i;
relevant[i] = new_integer;//insert integer into vector
lattice[0][i] = &relevant[i];//let pointer point onto this value
}
//OUTPUT
for(int j = 0; j<10; j++){
cout<<*lattice[0][j]<<" ";
cout<<relevant[j]<<endl;
}
这篇关于将指针解引用为向量的奇怪输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!