gpt4 book ai didi

c++ - 使用 const_iterator 成员调用类的析构函数时堆损坏

转载 作者:搜寻专家 更新时间:2023-10-31 01:32:49 24 4
gpt4 key购买 nike

我有一个包含字符串双端队列和迭代器作为成员的类。有一个 GetNext() 方法可以为我提供下一个元素并递增迭代器。使用来自外部的类,我检查一个空字符串以指示双端队列的结尾。不幸的是,当调用构造函数并且使用 GetNext 获取最后一个元素时,我遇到了堆损坏。我想这是因为在双端队列中的最后一个字符串(空字符串)之后,迭代器仍然递增并指向内存中的某处?以便析构函数然后尝试释放此内存然后崩溃?

   #include <iostream>
#include <string>
#include <deque>


class Foo
{
public:
Foo()
{
list.push_back("first elm");
list.push_back("second elm");
list.push_back(std::string());
pNextItem = list.begin();
}

virtual ~Foo(){}

const std::string& GetNext() { return *pNextItem++; }

protected:

std::deque<std::string> list;
std::deque<std::string>::const_iterator pNextItem;
};


int main()
{
{
Foo foo;
std::cout << foo.GetNext() << std::endl; // "first elm"
std::cout << foo.GetNext() << std::endl; // "second elm"
std::cout << foo.GetNext() << std::endl; // ""
//third call sets the iterator past the last element and causes a segfault
std::cout << foo.GetNext() << std::endl;

}

}

举个例子: Compilable and executable Example

最佳答案

在取消引用之前,您需要检查 pNextItem 是否等于 list.cend()。这正是在第三次调用 foo.GetNext() 之后发生的情况,此时您将取消引用最后一个元素并递增迭代器。

来自 deque::end reference :

Returns an iterator to the element following the last element of the container.

This element acts as a placeholder; attempting to access it results in undefined behavior.

关于c++ - 使用 const_iterator 成员调用类的析构函数时堆损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42506633/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com