我在以下代码(指针)上收到 SIGSEGV 错误,但不知道我哪里出了问题



这是我正在构建的代码(并解释我的任务(:

/*
Proposal:
Create a list-based graph representation.
It will need to support the following operations.
1. Ask the user how many Vertices there are.
2. Ask the user to label those Vertices, ie "A", "B", "C"...
3. Define the graph as an array of linked lists based on the number of
Vertices that hold labels. The node type would support the label and weight.
4. Repeatedly ask the user to define edges between two Vertices with the weight of the edge. Add these edges to the list.
5. Have a list method that will list out all of the edges in the graph
with their weights.
Sample Run:
N/A
*/
#include <iostream>
#include <cstdlib>
#include <string>

// ========== Struct Definition(s) ==========
struct AdjListNode {
int dest;
std::string label;
int weight;
struct AdjListNode* next;
};

struct AdjList {
struct AdjListNode *head;
};

// ===== Class Definition(s) ==========
class Graph {
private:
int num;
struct AdjList* array;
public:
std::string *a;
Graph(int);
void addEdge(int, int, int, std::string, std::string);
void label(std::string,int);
void output();
AdjListNode* newAdjListNode(int point, int weight, std::string labelname) {
AdjListNode* newNode = new AdjListNode;
newNode->dest = point;
newNode->weight = weight;
newNode->label = labelname;
newNode->next = NULL;
return newNode;
}
};

// ========== Function Prototype(s) ==========
// In Class(es)

// ========== Main Function ==========
int main() {
int num, distance, choice, head, tail;
std::string input;
std::cout << "Enter the number of vertices: ";
std::cin >> num;
Graph graph(num);
std::cout << "nPlease label the vertices ('A', 'B', 'C', etc.)n";
for (int i = 0; i < num; i++) {
std::cout << "Label for vertex " << (i+1) << ": ";
std::cin >> input;
graph.label(input, i);
}
for (int i = 0; i < num; i++) {
std::cout << "nEnter two edges between " << "OUTPUT i and i+1 vertices (e.g. 1 5): ";
std::cin >> head >> tail;
std::cout << "Enter the weight of this edge: ";
std::cin >> distance;
graph.addEdge(head, tail, distance, graph.a[head], graph.a[tail]);
}
graph.output();
return 0;
}

// ========== Function Definition(s) ==========
Graph::Graph(int num) {
this->num = num;
array = new AdjList [num];
a = new std::string [num];
for (int i = 0; i < num; ++i) {
array[i].head = NULL;
}
}

void Graph::addEdge(int a, int b, int weight, std::string bname, std::string tname) {
if( a >= num || b >= num || a < 0 || b < 0) {
std::cout << " Input is invalidn";
}
else {
AdjListNode* newNode = newAdjListNode(b,weight, tname);
newNode->next = array[a].head;
array[a].head = newNode;

newNode = newAdjListNode(a,weight, bname);
newNode->next = array[b].head;
array[b].head = newNode;
}
}

void Graph::label(std::string b, int i) {
a[i] = b;
}

void Graph::output() {
std::string input;
for (int i = 0; i < num; i++) {
AdjListNode* list = array[i].head;
std::cout << "nAdjacency list of the vertex " << i << " with labeled name: " << a[i];
while (list) {
std::cout << list->dest << " with the labeled name " << list->label << " and the weight being " << list->weight << "n";
list = list->next;
}
std::cout << "n";
}
}

当我点击这一行时,我收到了错误:graph.addEdge(head, tail, distance, graph.a[head], graph.a[tail]);,然而,我怀疑问题在于我的类定义及其设置方式。

有人能在这里帮我吗?

我在这里运行代码(onlinegdb(

阻止我的是,我稍微了解SIGSEGV错误是什么,但我就是看不出我在哪里错误地尝试访问内存。调试器显示CCD_ 2具有以下内容->{num = 2, array = 0x614c20, a = 0x614c48},但内存中的这些点应该是可访问的,因为我正在保留,不是吗?

graph值的说明-我正在输入2顶点,对于前2条边,我正在输入权重为515

以下是运行程序时的确切I/O:

Enter the number of vertices: 2                                                                                                                                     
                                                                                              
Please label the vertices ('A', 'B', 'C', etc.)                                                                                                                     
Label for vertex 1: A                                                                                                                                               
Label for vertex 2: B                                                                                                                                               
                                                                                              
Enter two edges between OUTPUT i and i+1 vertices (e.g. 1 5): 1 5                                                                                                   
Enter the weight of this edge: 2                                                                                                                                    
Segmentation fault (core dumped)                                                                                                                                    
                                                                                              
                                                                                              
...Program finished with exit code 139                                                                                                                              
Press ENTER to exit console.

graph.a有两个元素,但tail设置为5,而graph.a[tail]是越界访问。它会导致未定义的行为。

您应该避免动态内存分配,而是使用std::vectorstd::vector提供了std::vector::at来帮助查找这些错误。你用表现来支付。std::vector::at增加了范围检查,速度有点慢,但搜索bug会让速度变慢。

#include <iostream>
#include <string>
#include <vector>
struct AdjListNode {
int dest;
std::string label;
int weight;
AdjListNode* next;
};
struct AdjList {
AdjListNode *head;
};
class Graph {
private:
int num;
std::vector<AdjList> array;
public:
std::vector<std::string> a;
Graph(int);
void addEdge(int, int, int, std::string, std::string);
void label(std::string,int);
void output();
AdjListNode* newAdjListNode(int point, int weight, std::string labelname) {
AdjListNode* newNode = new AdjListNode;
newNode->dest = point;
newNode->weight = weight;
newNode->label = labelname;
newNode->next = nullptr;
return newNode;
}
};
int main() {
int num, distance, head, tail;
std::string input;
std::cout << "Enter the number of vertices: ";
std::cin >> num;
Graph graph(num);
std::cout << "nPlease label the vertices ('A', 'B', 'C', etc.)n";
for (int i = 0; i < num; i++) {
std::cout << "Label for vertex " << (i+1) << ": ";
std::cin >> input;
graph.label(input, i);
}
for (int i = 0; i < num; i++) {
std::cout << "nEnter two edges between " << "OUTPUT i and i+1 vertices (e.g. 1 5): ";
std::cin >> head >> tail;
std::cout << "Enter the weight of this edge: ";
std::cin >> distance;
graph.addEdge(head, tail, distance, graph.a.at(head), graph.a.at(tail));
}
graph.output();
return 0;
}
Graph::Graph(int num) : array(num), a(num) {
this->num = num;
for (int i = 0; i < num; ++i) {
array.at(i).head = nullptr;
}
}
void Graph::addEdge(int a, int b, int weight, std::string bname, std::string tname) {
if( a >= num || b >= num || a < 0 || b < 0) {
std::cout << " Input is invalidn";
} else {
AdjListNode* newNode = newAdjListNode(b,weight, tname);
newNode->next = array.at(a).head;
array.at(a).head = newNode;

newNode = newAdjListNode(a,weight, bname);
newNode->next = array.at(b).head;
array.at(b).head = newNode;
}
}
void Graph::label(std::string b, int i) {
a.at(i) = b;
}
void Graph::output() {
std::string input;
for (int i = 0; i < num; i++) {
AdjListNode* list = array.at(i).head;
std::cout << "nAdjacency list of the vertex " << i << " with labeled name: " << a.at(i);
while (list) {
std::cout << list->dest << " with the labeled name " << list->label << " and the weight being " << list->weight << "n";
list = list->next;
}
std::cout << "n";
}
}

引发异常:

terminate called after throwing an instance of 'std::out_of_range'
what():  vector::_M_range_check: __n (which is 5) >= this->size() (which is 2)

最新更新