【问题标题】:2 [main] hw3 10368 cygwin_exception::open_stackdumpfile: Dumping stack trace to hw3.exe.stackdump2 [main] hw3 10368 cygwin_exception::open_stackdumpfile:将堆栈跟踪转储到 hw3.exe.stackdump
【发布时间】:2018-04-20 00:36:58
【问题描述】:
#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->left->value 是不可能的吗?通过打印语句,我推断这是错误。我不太了解指针,希望得到帮助。 :)

【问题讨论】:

  • root1-&gt;level=0 这里,root1 是一个未初始化的指针,指向内存中的某个随机地址。通过访问它,您的程序会表现出未定义的行为。

标签: c++ pointers struct exception-handling cygwin


【解决方案1】:

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

...

// 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 的值(即它所指的地址)是undefined (可以是 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 除外,您没有动态分配它)。

【讨论】:

  • 是的,我傻得要命
  • 别这么不礼貌,对自己也不行。 @ImaGoon
  • @ImaGoon - 不要出汗。大多数人一开始很难用指针。如果我让它听起来很明显,那只是因为我使用 C/C++ 已经 30 多年了。 :-)
猜你喜欢
  • 1970-01-01
  • 2015-04-25
  • 1970-01-01
  • 2021-07-02
  • 2020-12-04
  • 1970-01-01
  • 2019-08-06
  • 2013-10-06
  • 2017-12-31
相关资源
最近更新 更多