【发布时间】: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->level=0这里,root1是一个未初始化的指针,指向内存中的某个随机地址。通过访问它,您的程序会表现出未定义的行为。
标签: c++ pointers struct exception-handling cygwin