gpt4 book ai didi

c++ - Delete 不识别 List 中的指针分配

转载 作者:行者123 更新时间:2023-11-28 00:04:10 26 4
gpt4 key购买 nike

当我尝试在指向 struct Vertex 的指针上调用 delete(使用 Vertex * v = new Vertex 分配,然后成功使用并存储在 std 中::list 在我的类析构函数中,我得到了这个运行时错误:

graphtake3(12325,0x100082000) malloc: *** error for object 0x100200340: pointer being freed was not allocated
***

指针肯定被分配了,因为应用程序运行良好,一切都在堆栈跟踪中显示,但由于某种原因,delete 似乎无法释放它们。这里发生了什么,为什么不删除工作?

这里是相关的缩写代码:

#include <vector>
#include <list>
#include <iostream>
#include <string>

enum Color {BLACK, GREY, WHITE};

struct Vertex {
int id;
std::string name;
Color color;

Vertex();
Vertex(std::string name);

~Vertex();

};
class Graph {
std::vector<std::list<Vertex *>> adjList;

public:
Graph();
Graph (int nodeCount);

~Graph();

int newVertex();
int newVertex(std::string name);
void newUnDirectedEdge(int v1, int v2);
void newDirectedEdge(int v1, int v2);
std::list<Vertex*> getConnections(int v);

friend std::ostream& operator<<(std::ostream& os, const Graph& g);



};

#include "Graph.hpp"

Vertex::Vertex() {
color = WHITE;
}

Vertex::Vertex(std::string name) {
this->name = name;
color = WHITE;
}

Vertex::~Vertex() {

}

Graph::Graph() {

}

Graph::Graph(int nodeCount) {
adjList.reserve(nodeCount);
}

Graph::~Graph(){
for (int i = 0; i<adjList.size(); i++) {

for (std::list<Vertex*>::iterator iterator = adjList[i].begin(), end = adjList[i].end(); iterator !=end; iterator++) {
delete (*iterator); //fails
}
}
}

int Graph::newVertex() {
Vertex * v = new Vertex();
adjList.push_back(std::list<Vertex *>(1, v));
v->id= (int)adjList.size()-1;
return v->id;
}

int Graph::newVertex(std::string name) {
Vertex * v = new Vertex();
adjList.push_back(std::list<Vertex *>(1, v));
v->id= (int)adjList.size()-1;
v->name= name;
return v->id;
}

void Graph::newUnDirectedEdge(int v1, int v2) {
newDirectedEdge(v1, v2);
newDirectedEdge(v2, v1);
}

void Graph::newDirectedEdge(int v1, int v2) {
Vertex * vertex2 = adjList[v2].front();
adjList[v1].push_back(vertex2);

}
std::list<Vertex*> Graph::getConnections(int v) {
return adjList[v];
}

std::ostream& operator<<(std::ostream& os, const Graph& g) {
for (int i = 0; i<g.adjList.size(); i++) {
for (std::list<Vertex*>::const_iterator iterator = g.adjList[i].begin(), end = g.adjList[i].end(); iterator !=end; iterator++) {
os << (*iterator)->id << " (" << (*iterator)->name << ") ";
}
os << '\n';
}

return os;
}

与主要:

#include <iostream>
#include "Graph.hpp"


int main(int argc, const char * argv[]) {
Graph graph(5);

int v1 = graph.newVertex("Paris");
int v2 = graph.newVertex("London");
int v3 = graph.newVertex("Lyon");
int v4 = graph.newVertex("Nice");
int v5 = graph.newVertex("Marseille");
int v6 = graph.newVertex("La Rochelle");
int v7 = graph.newVertex("Toulon");

graph.newUnDirectedEdge(v2, v1);
graph.newUnDirectedEdge(v1, v3);
graph.newUnDirectedEdge(v1, v4);
graph.newUnDirectedEdge(v3, v4);
graph.newUnDirectedEdge(v5, v4);
graph.newUnDirectedEdge(v7, v5);

std::cout << graph;


return 0;
}

最佳答案

一旦你这样做,你就准备好迎接灾难了:

void Graph::newDirectedEdge(int v1, int v2) {
Vertex * vertex2 = adjList[v2].front();
adjList[v1].push_back(vertex2);
}

问题是你没有区分谁拥有一个指针。在这种情况下,您只是将指针复制到列表中。但是,当您转到 ~Graph 时,您删除了列表中的所有 指针。有的是通过new获取的,有的是通过上面的函数复制过来的。

所以错误是对的:指针没有分配。发生的事情是你已经删除了它,然后又删除了它的拷贝。

您需要重新考虑您的设计并考虑指针所有权。或者您可以使用将所有内容转换为 std::shared_ptr 的大锤方法。但我实际上并不推荐这样做。

图形的一种常见方法是存储所有顶点(在 std::vector 中),然后像您所做的那样在单独的结构中连接。然后在析构函数中,你只需撕开 vector 。您甚至可以借此机会学习如何使用 std::unique_ptr。 =)

关于c++ - Delete 不识别 List 中的指针分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36686362/

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