C++ 二叉树C/C++;返回的进程-1073741819(0xC0000005)

C++ 二叉树C/C++;返回的进程-1073741819(0xC0000005),c++,segmentation-fault,C++,Segmentation Fault,我是C/C++编程新手。我正在尝试编写二叉树代码,并找到它的前序、后序和有序结构。到目前为止,我在3级子树方面做得很好,但是当我尝试添加更多子级(4级)时,我得到了“processreturned-1073741819(0xC0000005)”错误。我知道这是内存分配违规,我做了一些研究,但说真的,我不知道如何修复它。这是我的密码 #include <iostream> #include <iostream> #include <string> #includ

我是C/C++编程新手。我正在尝试编写二叉树代码,并找到它的前序、后序和有序结构。到目前为止,我在3级子树方面做得很好,但是当我尝试添加更多子级(4级)时,我得到了“processreturned-1073741819(0xC0000005)”错误。我知道这是内存分配违规,我做了一些研究,但说真的,我不知道如何修复它。这是我的密码

#include <iostream>
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

struct node
{
    string data;
    struct node* left;
    struct node* right;
};

/* allocates a new node with the NULL left and right pointers. */
struct node* newNode(string data)
{
    struct node* node = (struct node*)
    malloc(sizeof(struct node));
    node->data = data;
    node->left = NULL;
    node->right = NULL;

    return(node);
}

/* Given the tree, print nodes, postorder traversal. */
void printPostorder(struct node* node)
{
    if (node == NULL)
    return;

    // first recur on left subtree
    printPostorder(node->left);
    // then recur on right subtree
    printPostorder(node->right);
    // now deal with the node
    // printf("%d ", node->data);
    cout << node->data;
}

/* print nodes in inorder*/
void printInorder(struct node* node)
{
    if (node == NULL)
    return;
    /* first recur on left child */
    printInorder(node->left);
    /* then print the data of node */
    // printf("%d ", node->data);
    cout << node->data;
    /* now recur on right child */
    printInorder(node->right);
}

/* print nodes in preorder*/
void printPreorder(struct node* node)
{
    if (node == NULL)
    return;
    /* first print data of node */
    // printf("%d ", node->data);
    cout << node->data;
    /* then recur on left sutree */
    printPreorder(node->left);
    /* now recur on right subtree */
    printPreorder(node->right);
}

int main()
{
    struct node *root = newNode("A");
    root->left = newNode("B");
    root->right = newNode("C");
    root->left->left = newNode("D");
    root->left->right = newNode("E");
    root->right->left = newNode("F");
    root->right->right = newNode("G");
    root->left->right->left = newNode("H");
    root->left->right->right = newNode("I");
    root->right->left->left = newNode("J"); // if i delete this, all is fine
    root->right->left->right = newNode("K"); // if i delete this, all is fine

    printf("\n Preorder traversal of binary tree is \n");
    printPreorder(root);
    printf("\n Inorder traversal of binary tree is \n");
    printInorder(root);
    printf("\n Postorder traversal of binary tree is \n");
    printPostorder(root);

    return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
结构节点
{
字符串数据;
结构节点*左;
结构节点*右;
};
/*使用空的左指针和右指针分配新节点*/
结构节点*新节点(字符串数据)
{
结构节点*节点=(结构节点*)
malloc(sizeof(struct node));
节点->数据=数据;
节点->左=空;
节点->右=空;
返回(节点);
}
/*给定树、打印节点、后序遍历*/
作废printPostorder(结构节点*节点)
{
if(node==NULL)
返回;
//左子树上的第一次递归
printPostorder(节点->左侧);
//然后在右子树上重现
printPostorder(节点->右侧);
//现在处理节点
//printf(“%d”,节点->数据);
cout数据;
}
/*按顺序打印节点*/
无效打印顺序(结构节点*节点)
{
if(node==NULL)
返回;
/*第一次复发于左侧儿童*/
打印顺序(节点->左侧);
/*然后打印节点的数据*/
//printf(“%d”,节点->数据);
cout数据;
/*现在在正确的孩子身上重现*/
打印顺序(节点->右侧);
}
/*按预订单打印节点*/
无效打印预订单(结构节点*节点)
{
if(node==NULL)
返回;
/*首先打印节点的数据*/
//printf(“%d”,节点->数据);
cout数据;
/*然后在左sutree上重现*/
打印预订单(节点->左侧);
/*现在在右子树上重现*/
打印预订单(节点->右侧);
}
int main()
{
结构节点*根=新节点(“A”);
根->左=新节点(“B”);
根->右=新节点(“C”);
根->左->左=新节点(“D”);
根->左->右=新节点(“E”);
根->右->左=新节点(“F”);
根->右->右=新节点(“G”);
根->左->右->左=新节点(“H”);
根->左->右->右=新节点(“I”);
root->right->left->left=newNode(“J”);//如果我删除这个,一切都很好
root->right->left->right=newNode(“K”);//如果我删除这个,一切都很好
printf(“\n二叉树的前序遍历是\n”);
打印预订单(根);
printf(“\n二叉树的顺序遍历为\n”);
打印顺序(根);
printf(“\n二叉树的后序遍历是\n”);
打印邮购(根);
返回0;
}

对不起,我的英语不好,希望你们都能理解。提前感谢:)

一个主要问题和未定义行为(可能导致崩溃)的来源是,您正在使用
malloc
分配结构。问题是它实际上并不构造对象,它只是分配内存。这意味着节点中的字符串成员将无法正确构造,从而导致上述未定义行为

在C++中分配内存时,任何类型的内存都应该使用<代码>新< /C> >:

node* node = new struct node;

注意:您必须在此处使用
struct
关键字,因为您有一个类型和一个同名变量。

一个主要问题和未定义行为(可能导致崩溃)的根源是您正在使用
malloc
分配您的结构。问题是它实际上并不构造对象,它只是分配内存。这意味着节点中的字符串成员将无法正确构造,从而导致上述未定义行为

在C++中分配内存时,任何类型的内存都应该使用<代码>新< /C> >:

node* node = new struct node;

注意:您必须在此处使用
struct
关键字,因为您有一个类型和一个同名变量。

您应该
释放所有分配给启动器的内存。这是崩溃。您应该在调试器中运行以捕获它,调试器将在崩溃位置停止,让您检查调用堆栈(如果需要,也可以检查调用堆栈)和变量值。您应该
释放为启动器分配的所有内存。这就是崩溃。您应该在调试器中运行以捕获它,调试器将在崩溃位置停止,让您检查调用堆栈(如果需要,也可以检查调用堆栈)以及变量值。或者,如果打算延迟构造,可以使用placement new:
void*ptr=malloc(size);新(ptr)类型(args),那么,在C++中使用MaloC?<代码>是不好的做法(不仅是坏的实践,而且如果你希望某些构造函数运行,那就错了)。在C语言中,这是可以的(但这是另一种语言)。@user2869359在这种情况下,这甚至不是一种坏的做法,这是完全错误的。如果你系统地使用
new
而不是
malloc
,即使是非常简单的情况,你也是安全的,。。。谢谢你指出这一点。那时我还得做更多的研究。干杯:)!或者,如果打算延迟施工,可以使用新的布局:
void*ptr=malloc(尺寸);新(ptr)类型(args),那么,在C++中使用MaloC?<代码>是不好的做法(不仅是坏的实践,而且如果你希望某些构造函数运行,那就错了)。在C语言中,这是可以的(但这是另一种语言)。@user2869359在这种情况下,这甚至不是一种坏的做法,这是完全错误的。如果你系统地使用
new
而不是
malloc
,即使是非常简单的情况,你也是安全的,。。。谢谢你指出这一点。那时我还得做更多的研究。干杯:)!