释放动态分配的内存时程序崩溃
Program crashes while freeing dynamically allocated memory
我在这里使用邻接列表来表示一个图。
这是代码:
// A program to check the reachability between two nodes within a specified number of steps using an adjacency list
/***************************************************************************/
#include <iostream>
#include <fstream>
using namespace std;
struct node {
int value;
node* next;
// Constructors:
node() {
value = 0;
next = 0;
}
node(int x) {
value = x;
next = 0;
}
};
node* al; // Adjacency list
int n; // Number of nodes
int node1, node2, k; // For reading input from the console
int counter;
bool checkReachability(int, int, int);
void freeMemory();
int main() {
ifstream in;
in.open("Input.txt");
if(in) {
in >> n;
al = new node[n];
for (int i = 0; i < n; i++) {
al[i].value = i+1;
}
int a, b;
while(in >> a >> b) {
node* temp = &al[a-1];
while(temp->next != 0) {
temp = temp->next;
}
temp->next = new node(b);
}
cout << "nnThe adjacency list representation of the graph is as follows: n";
cout << "________________________________nn";
for (int i = 0; i < n; i++) {
cout << al[i].value;
node* temp = al[i].next;
while(temp != 0) {
cout << "->" << temp->value;
temp = temp->next;
}
cout << endl;
}
cout << "________________________________n";
in.close();
char c;
do {
cout << "nPlease enter the input (node1, node2, k): n";
cin >> node1 >> node2 >> k;
counter = 0;
if (checkReachability(node1 - 1, node2, k)) {
cout << "nReachable within " << k << " steps";
if (counter < k) {
cout << " (actually " << counter << ")";
}
cout << endl << endl;
}
else {
cout << "nNot reachable within " << k << " steps n";
}
cout << "nDo you want to continue? Y/N nn";
cin >> c;
} while (c == 'Y' || c == 'y');
freeMemory();
} else {
cout << "nCouldn't find the input filenn";
}
return 0;
}
bool checkReachability(int n1, int n2, int k) {
if ((n1 + 1) == n2) return true;
counter++;
if (counter <= k) {
node* temp = &(al[n1]);
while (temp != 0) {
if (temp->value == n2) return true;
temp = temp->next;
}
temp = al[n1].next;
while (temp != 0) {
if (checkReachability(((temp->value)-1),n2,k)) return true;
counter--;
temp = temp->next;
}
}
return false;
}
void freeMemory() {
cout << "nFreeing memory...n";
// To free the dynamically allocated memory on the heap
for (int i = 0; i < n; i++) {
node* temp = &al[i];
while(temp != 0) {
node* temp2 = temp;
temp = temp->next;
delete temp2;
}
}
//delete [] al;
cout << "nMemory freed.n";
}
这个程序运行得很好。只有当我选择退出它时,它才会崩溃,因为它调用了freeMemory函数。请帮我弄清楚问题出在哪里。
Input.txt文件:
5
1 2
2 5
3 4
1 3
输出:
The adjacency list represent
____________________________
1->2->3
2->5
3->4
4
5
____________________________
Please enter the input (node
1 2 1
Reachable within 1 steps
Do you want to continue? Y/N
y
Please enter the input (node
2 4 4
Not reachable within 4 steps
Do you want to continue? Y/N
N
Freeing memory...
然后,它崩溃了。
这是错误的:
void freeMemory() {
cout << "nFreeing memory...n";
// To free the dynamically allocated memory on the heap
for (int i = 0; i < n; i++) {
node* temp = &al[i]; // HERE
while(temp != 0) {
node* temp2 = temp;
temp = temp->next;
delete temp2;
}
}
delete [] al;
cout << "nMemory freed.n";
}
初始矢量CCD_ 1是通过CCD_。意味着所有时隙a1[0...n-1]
中的初始条目是向量分配的一部分;而不是此后与每个节点相关联的链式邻接序列的一部分。我相信你需要这样做:
void freeMemory() {
cout << "nFreeing memory...n";
// To free the dynamically allocated memory on the heap
for (int i = 0; i < n; i++) {
node* temp = al[i].next; // start with next pointer
while(temp != 0) {
node* temp2 = temp;
temp = temp->next;
delete temp2;
}
}
delete [] al;
cout << "nMemory freed.n";
}
或者,您可以从一开始就使用指针数组,并动态地单个分配所有节点,而不仅仅是相邻链,此时用于释放的循环可以工作,但代码的其余部分需要一些修改。考虑到你在这方面的进展,我只会做出我上面展示的改变,并称之为好。
相关文章:
- 程序崩溃并显示"std::out_of_range"错误
- 试图创建流或fopen时程序崩溃
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 如何找出应用程序崩溃的原因 - Win 10 LTSB
- 操纵安卓相机的深度图导致应用程序崩溃
- 为什么当我尝试搜索双链表中第一个数据条目之外的数据时,程序崩溃了?
- DLL Made with CMake 使程序崩溃
- 程序崩溃使用boost::asio
- 调用 free() 有时会导致程序崩溃
- 邮件加密程序崩溃
- 调用 java 的回调() 时应用程序崩溃.由于 detatchThread 而获得运行时错误
- 比较迭代器会使程序崩溃,而不会在自定义气泡排序实现中出现错误
- For 循环在尝试读取数组 c++ 时程序崩溃
- 即使有 0 个错误,Getter 似乎也会使程序崩溃
- Windows桌面程序保存您的计算机会话 - 基于程序崩溃时的恢复会话
- 无法访问的代码如何导致我的程序崩溃?
- 矢量迭代器在尝试调用函数时使我的程序崩溃
- QCompleter set模型使应用程序崩溃
- 使用唯一指针调用函数会使我的程序崩溃
- 删除SDL_PollEvent时程序崩溃