2 [main] hw3 10368 cygwin_exception::open_stackdumpfile:将堆栈跟
2 [main] hw3 10368 cygwin_exception::open_stackdumpfile: Dumping stack trace to hw3.exe.stackdump
#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
#include <utility>
using namespace std;
struct node
{
int level = -1;
int value = -5;
node *left;
node *right;
};
int array[100][100];
void storetree(node *root, int val);
void printtree();
int main()
{
cout << " u there?" << endl;
for (int i = 0; i < 100; i++)
{
for (int j = 0; j < 100; j++)
{
array[i][j] = -5;
}
}
ifstream file1;
ifstream file2;
file1.open("tree1.txt");
file2.open("graph1.txt");
node root;
node *root1;
int val1;
int randval;
file1 >> val1;
root.level = 0;
root1->level = 0;
root.value = val1;
root1->value = val1;
array[0][0] = val1;
cout << "made it" << endl;
root1->left->value = -5; // <-- Error happens here
root1->right->value = -5;
root1->left->level = -5;
root1->right->level = -5;
因此,当我访问root1->left->value时,会发生错误,并且出现堆栈转储错误。
以我编写的方式访问 root1->左>值是不可能的吗?通过打印语句,我推断出这是错误。我不太了解指针,希望得到帮助。:)
我在下面注释了您的源代码:
...
// Allocate an actual instance of your 'node' struct (local variable, on the stack)
node root;
// This allocates a POINTER variable, which just contains an address.
// The compiler knows that 'root1' will point at 'node' types.
// Note that you've only created a pointer variable here; NOT an actual node.
// Further, you haven't initialized it, so its value is "undefined"
// (you can't assume you know what its value is ... it could be anything).
node *root1;
...
// Set 'level' on the root node to zero
root.level = 0; // This is OK, because root is an actual 'node' struct instance
// Set 'level' ON THE NODE THAT 'root1' POINTS TO to zero
root1->level = 0; // This is not OK, because root1 is a wild (uninitialized) pointer
这是你的第一个问题。你创建了一个指针,但你没有指向任何东西。root1
的值(即它引用的地址)是未定义的(可以是 NULL,可以是该变量所在的内存中的任何内容)
最好在定义局部变量时立即对其进行初始化。未初始化的变量可以具有未定义的值,这可能会为您的生活增加无数小时的调试时间,其中程序的每次运行都与上次运行不同。嘭。
如果在定义变量时尚未准备好分配实际值, 将其设置为某个已知值,例如 0、nullptr
等。如果你以后忘记设置它,你的程序至少每次都会做同样的错误事情。
node *root1 = nullptr; // (...or maybe your compiler uses "NULL" or "(void *)0"...)
看起来您将根据从输入文件中读取的任何内容构建节点树? 如果是这样,那么您几乎肯定会动态分配节点结构,因为您无法提前知道需要多少节点结构。
// Allocate and initialize a new node struct (on the heap)
root1 = new node();
// Set 'level' ON THE NODE THAT 'root1' POINTS TO to zero
root1->level = 0; // Yay, now this works
变量root1
现在包含新分配的node
结构的地址。其余代码应该从那里开始工作。
只是提醒一下,一个"正确"的程序(例如,一个不泄漏内存的程序)最终应该在调用new
返回的每个指针上调用delete
。 此外,在构建动态分配的node
对象树时,请记住这一点;完成后,您需要对它们中的每一个调用delete
(除了 root,它不是动态分配的)。
相关文章:
- Poco::Net::FTPClientSession 在 open() 方法上挂起 129 秒,如果 ftp 主机不存
- Node.js fs.open() 在尝试打开 4 个以上的命名管道 (FIFO) 后挂起
- POSIX open() 挂在 SMB 共享上
- C++:std::ofstream 方法 open() 在第二次迭代时擦除打开的 ifstream 文件
- Visual Studio "fatal error LNK1104: cannot open file"未使用或包含的库
- C++ 错误检查 fstream open() 命令和一般字符串流错误处理
- VS 2017 C++ - "cannot open source file 'sqlite3.h' "
- 在VS 15.7.5中,悬停在Open CV 3.4.1Mat上仍然冻结
- 计算着色器Open GL ES的多个输入
- 从生成文件编译错误:"Unable to open output file" ..."No such file or directory"
- Open GL ES 3.1 的计算着色器的最小工作示例
- 我可以对"int 文件 = open(path, flag);"做点什么吗?
- 进口。TLB文件给出"cannot open source file x.tlh"
- QtCreator 中的"Fatal Error C1083: Cannot open include file"
- OpenCV: fs.open fails
- Open AL 的函数在 ubuntu 中使用 g++ 编译时给出未定义引用的错误
- 标准::Ofstream Open的Klocwork问题
- Open() 用于创建文件和打开现有文件.有人能分辨出其中的区别
- 成功使用CMakeLists制作后如何修复"open: No such file or directory"错误(该文件实际上存在)
- C++ OPEN SSL 库 HMAC 功能返回值每次运行时都不相同