- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在这里使用邻接表表示一个图。
代码如下:
// 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 << "\n\nThe adjacency list representation of the graph is as follows: \n";
cout << "________________________________\n\n";
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 \n\n";
cin >> c;
} while (c == 'Y' || c == 'y');
freeMemory();
} else {
cout << "\nCouldn't find the input file\n\n";
}
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 函数。请帮助我找出问题所在。
输入.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";
}
初始 vector a1
是通过new node[n]
分配的。这意味着所有插槽 a1[0...n-1]
中的初始条目都是 vector 分配的一部分;不属于此后与每个节点相关联的链接邻接序列的一部分。我相信你需要这样做:
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";
}
或者,您可以从开始使用指针数组并动态地单分配所有节点,而不仅仅是邻接链,此时您的释放循环将起作用,但您的其余代码将需要进行一些更改。考虑到您对此的支持程度,我只会进行上面显示的更改并称其为好。
关于c++ - 程序在释放动态分配的内存时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28663768/
我有一个附加了 View Controller 的 AVAudioPlayer 实例。 @property (nonatomic, retain) AVAudioPlayer *previewAudi
我是java初学者。假设我声明了一个 Account 类型的变量 Account _account = new Account("Thomas"); 然后在其他地方我做了这样的事情: _account
我在我的应用程序中使用了 3 个 UIViewController,现在我想知道当我从另一个应用程序切换到另一个 UIViewController 时释放它们是否是一个好主意。显然,这将是隐藏的,当它
我分配了一个直接缓冲区: ByteBuffer directBuffer = ByteBuffer.allocateDirect(1024); 我读过: Deallocating Direct Buf
场景。我有一个图表,我可以使用右键单击来执行平移。这非常有效。然后我完美地添加了右键菜单。 问题。现在,即使在拖动操作完成后释放鼠标,也会显示右键菜单。 有没有办法在 Java Swing 或 Jav
我使用此代码获取 ABPerson 的姓氏 CFStringRef lastNameRef = ABRecordCopyValue((ABRecordRef)personRecordRef, kABP
目前,我们在基于 C 的嵌入式应用程序中使用 malloc/free Linux 命令进行内存分配/取消分配。我听说这会导致内存碎片,因为内存分配/取消分配会导致堆大小增加/减少,从而导致性能下降。其
当我尝试释放缓冲区时遇到问题。每次我尝试将缓冲区传递给释放方法时,都会发生段错误。 Valgrind 确认段错误位于 BufferDeallocate 方法中。 ==30960== Memcheck,
我想知道何时按下或释放修改后的键(Ctrl 或 Shift)。 基本上,用户可以在按下修改键的情况下执行多次击键,而我不想在它被释放之前执行一个操作(想想 Emacs 和 Ctrl + X + S).
我编写了一个相当大的网络应用程序。它运行良好一段时间,然后慢慢开始运行缓慢,因为 DOM 节点开始爬升到 80,000 - 100,000 左右。 所以我一直在 Chrome 开发工具控制台 (DCT
我知道在像 c 这样的语言中,我需要在分配内存后释放它。 (我来自 Java),对此我有几个问题: 当我在做的时候: int array[30]; (即创建一个大小为 30 个整数的数组)与
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: How to release pointer from boost::shared_ptr? Detach
我有一个可以从多个后台线程访问的类,可能同时访问。我无法复制该类,因为重新创建它的内容(处理或内存方面)可能很昂贵。 也有可能在后台处理仍在继续并访问该属性时替换了此类的属性。 目前我有定期的保留/释
这个问题是对: 的扩展链接-1:Creating an image out of the ios surface and saving it Link-2:Taking Screenshots fro
我有一个实例变量 NSMutableArray* searchResults。 首先,我初始化它: self.searchResults = [[NSMutableArray alloc] init]
如果我在堆上声明一些东西,比如 char *a=new char[1000] 并且主程序停止,如果没有 delete[]<,那么分配的内存会发生什么 调用?它保留在堆上还是自动释放? 最佳答案 就C+
在开发相机应用时,我遇到了一个异常,该异常仅在我切换到其他应用时发生(onPause() 用于我的应用)。 01-15 17:22:15.017: E/AndroidRuntime(14336): F
使用 JDK 1.8 编译时出现 maven 编译器错误 无法执行目标 org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (de
将 BufferedImage 保存到磁盘(以释放内存)的最快方法是什么? 我的 Java 应用程序处理大量图像(每约 300 毫秒将图像加载到内存中)。大多数这些图像都会立即被丢弃 (gc),但每隔
使用 JDK 1.8 编译时出现 maven 编译器错误 未能在项目 DUMMY 上执行目标 org.apache.maven.plugins:maven-compiler-plugin:3.8.1:
我是一名优秀的程序员,十分优秀!