简体   繁体   English

线程 1:向量插入上的 EXC_BAD_ACCESS(代码=1,地址=0x0)

[英]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:总而言之,所示代码中存在多个概念缺陷:

  1. All iterators must be initialized before they can be used in any way, incremented or decrementing, dereferenced, or in any other way.所有迭代器都必须先初始化,然后才能以任何方式使用,递增或递减,解除引用或以任何其他方式使用。

  2. The only possible valid iterator for a completely empty vector cannot be incremented or decremented.完全空向量的唯一可能有效迭代器不能递增或递减。

  3. 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.

相关问题 线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0)错误 - Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) error 代码错误:线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0) - code error : Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) 共同映射两个向量元素,并得到EXC_BAD_ACCESS(code = 1,address = 0x0)错误 - Comapring two vector elements and get EXC_BAD_ACCESS(code=1, address = 0x0) error 线程 1:xcode 中的 EXC_BAD_ACCESS(代码 = 1,地址 = 0x0)错误 - Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) error in xcode 线程1:生成EXC_BAD_ACCESS(代码= 1,地址= 0x0)问题 - Thread 1 : EXC_BAD_ACCESS (Code = 1, address = 0x0) issue is generated Xcode:线程1:制作邻接表时,EXC_BAD_ACCESS(代码= 1,地址= 0x0) - Xcode: Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) when making adjacency list 调用 glCreateShader 时出现运行时错误“线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x0)” - Getting runtime error "Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)" whencalling glCreateShader 线程 1:使用 scanf 时出现 EXC_BAD_ACCESS (code=1, address=0x0) 错误 - Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) error while using scanf 我的动态数组有问题 - 线程 1:EXC_BAD_ACCESS (code=1, address=0x0) - Problem with my dynamic array - Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) C ++节点分配错误:线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0) - C++ node assign error: Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM