[英]Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) on vector insert
I am trying to insert an element into a vector using the.insert(<#const_iterator __position#>, <#const_reference __x#>)我正在尝试使用 the.insert(<#const_iterator __position#>, <#const_reference __x#>) 将元素插入到向量中
This is my code:这是我的代码:
hpp:马力:
typedef int elementType;
class Heap{
private:
std::vector<elementType> myVecrtor;
int mySize = 1; //The minimum size is 1 since the first element is a dummy.
public:
Heap();
void insert(elementType const item);
};
cpp: cpp:
void Heap::insert(elementType item){
typename std::vector<elementType>::iterator it;
for(int i = 0; i <= mySize; i++){
it++;
}
myVecrtor.insert(it, item);
mySize++;
}
When calling the method inside the main:在主函数中调用方法时:
#include <iostream>
#include "Heap.hpp"
int main(int argc, const char * argv[]) {
Heap h;
h.insert(10);
}
The debugger gives me a successful run then gives me an error inside the vector hpp file:调试器让我成功运行,然后在矢量 hpp 文件中给我一个错误:
void __construct_backward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) {
ptrdiff_t _Np = __end1 - __begin1;
__end2 -= _Np;
if (_Np > 0)
_VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));//The error is given here
}
error:Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)
I have no idea why I am getting this.我不知道为什么我会得到这个。 Any help would be appreciated.
任何帮助,将不胜感激。
typename std::vector<elementType>::iterator it;
This statement declares a vector iterator.该语句声明了一个向量迭代器。 As you can see here, it is completely uninitalized.
正如您在此处看到的,它完全未初始化。 It is not initialized to anything.
它没有初始化为任何东西。
for(int i = 0; i <= mySize; i++){
it++;
}
And this sequence increments the iterator some unspecified number of times.并且此序列将迭代器递增一些未指定的次数。 Since
it
is not initialized, this is undefined behavior.因为
it
没有被初始化,所以这是未定义的行为。
myVecrtor.insert(it, item);
Attempting to insert something into a vector using an iterator that has never been initialized, and after being not-initialized it gets incremented an unspecified amount of times, is not going to go very far.尝试使用从未初始化过的迭代器将某些内容插入到向量中,并且在未初始化后它会增加未指定的次数,不会达到 go 太远。
Heap
's constructor is not shown, so it is unclear if its constructor initializes the vector's contents, in any form or fashion. Heap
的构造函数未显示,因此不清楚其构造函数是否以任何形式或方式初始化向量的内容。 But that seems unlikely and this mySize
appears to be initialized to 1. Therefore this for
loop will iterate at least once, and most likely not once but twice.但这似乎不太可能,而且这个
mySize
似乎被初始化为 1。因此这个for
循环将至少迭代一次,而且很可能不是一次而是两次。
If the vector is, indeed empty, then even if it
is correctly initialized, its only possible valid value will be either begin()
or end()
(with both being equivalent for a completely empty vector).如果向量确实是空的,那么即使
it
被正确初始化,它唯一可能的有效值也将是begin()
或end()
(对于一个完全空的向量来说两者都是等价的)。 As such, incrementing the iterator, even if it's properly initialized, will be undefined behavior.因此,增加迭代器,即使它被正确初始化,也将是未定义的行为。
To summarize, there are multiple conceptual flaws in the shown code:总而言之,所示代码中存在多个概念缺陷:
All iterators must be initialized before they can be used in any way, incremented or decrementing, dereferenced, or in any other way.所有迭代器都必须先初始化,然后才能以任何方式使用,递增或递减,解除引用或以任何其他方式使用。
The only possible valid iterator for a completely empty vector cannot be incremented or decremented.完全空向量的唯一可能有效迭代器不能递增或递减。
If insert()
's goal is to append a new value to a vector, nothing of what the shown code is doing is needed, in any form or fashion.如果
insert()
的目标是 append 一个向量的新值,则不需要显示的代码以任何形式或方式执行任何操作。 That's because this just happens to be exactly what std::vector
's very own push_back()
does, No iterator, or incrementing is required!那是因为这恰好正是
std::vector
自己的push_back()
所做的,不需要迭代器或递增!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.