C++ 2[main]hw3 10368 cygwin_异常::打开_stackdumpfile:将堆栈跟踪转储到hw3.exe.stackdump #包括 #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; 结构节点 { 整数级=-1; int值=-5; 节点*左; 节点*右; }; 整数数组[100][100]; void storetree(节点*根,int val); void printree(); int main() { cout-val1; root.level=0; root1->level=0; root.value=val1; root1->value=val1; 数组[0][0]=val1; cout value=-5;//右->值=-5; root1->left->level=-5; root1->right->level=-5;

C++ 2[main]hw3 10368 cygwin_异常::打开_stackdumpfile:将堆栈跟踪转储到hw3.exe.stackdump #包括 #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; 结构节点 { 整数级=-1; int值=-5; 节点*左; 节点*右; }; 整数数组[100][100]; void storetree(节点*根,int val); void printree(); int main() { cout-val1; root.level=0; root1->level=0; root.value=val1; root1->value=val1; 数组[0][0]=val1; cout value=-5;//右->值=-5; root1->left->level=-5; root1->right->level=-5;,c++,pointers,struct,exception-handling,cygwin,C++,Pointers,Struct,Exception Handling,Cygwin,因此,当我访问root1->left->value时,我的错误发生了,并且我得到了堆栈转储错误 是否无法以我编写的方式访问root1->left->value?通过print语句,我推断这就是错误。我不太理解指针,希望您能提供帮助。:)我已在下面注释了您的源代码: #include <stdio.h> #include <iostream> #include <string> #include <vector> #include <algor

因此,当我访问root1->left->value时,我的错误发生了,并且我得到了堆栈转储错误


是否无法以我编写的方式访问root1->left->value?通过print语句,我推断这就是错误。我不太理解指针,希望您能提供帮助。:)

我已在下面注释了您的源代码:

#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
(即它所指的地址)的值未定义 (可以为NULL,可以是该变量所在内存中的任何内容)

在定义局部变量时正确初始化它们是一种很好的做法。未初始化的变量可能有未定义的值,这会给你的生活增加无数个小时的调试时间,你的程序每次运行都会做一些与上一次不同的事情。Bleh

如果定义变量时未准备好分配实际值, 将它设置为某个已知值,如0、
nullptr
,不管怎样。如果您以后忘记设置它,您的程序至少每次都会做同样的错误

...

// 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
看起来您将根据从输入文件中读取的内容构建一个节点树? 如果是这样的话,那么您几乎肯定会动态分配节点结构,因为您不知道需要提前分配多少节点结构

node *root1 = nullptr;  // (...or maybe your compiler uses "NULL" or "(void *)0"...)
变量
root1
现在包含新分配的
node
struct的地址。剩下的代码应该从那里开始工作

只是提醒一下,一个“正确”的程序(例如,一个不泄漏内存的程序)最终应该对调用
new
返回的每个指针调用
delete

另外,在构建动态分配的
节点
对象树时,请记住这一点;您需要对每个对象调用
delete
(root除外,它不是动态分配的)完成后。

root1->level=0
这里,
root1
是一个未初始化的指针,指向内存中的某个随机地址。通过访问它,你的程序会表现出未定义的行为。是的,我非常愚蠢,不要这么不礼貌,甚至不要对自己@ImaGoon@ImaGoon-别担心。大多数人都有困难首先是指针。如果我让它听起来很明显,那只是因为我已经使用C/C++30多年了。:-)
// 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