【问题标题】:Tree Binary C/C++ Process returned -1073741819 (0xC0000005)树二进制 C/C++ 进程返回 -1073741819 (0xC0000005)
【发布时间】:2015-09-03 08:58:40
【问题描述】:

我是 C/C++ 编程的新手。我正在尝试编写二叉树代码并找到它的 PreOrder、PostOrder、InOrder 结构。到目前为止,我在 3 级子树上做得很好,但是当我尝试添加更多子树(4 级)时,我收到“进程返回 -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;
}

对不起,我的英语不好,希望大家理解。并提前感谢:)

【问题讨论】:

  • 你应该free所有为初学者分配的内存。
  • 那是一个崩溃。您应该在调试器中运行以捕获它,并且调试器将在崩溃的位置停止,让您检查调用堆栈(如果需要,也可以向上走)以及变量的值。

标签: c++ segmentation-fault


【解决方案1】:

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

当分配内存时,任何类型的,在 C++ 中你应该使用new:

node* node = new struct node;

注意:这里必须使用struct关键字,因为你有一个类型和一个同名的变量。

【讨论】:

  • 或者如果打算延迟施工,可以使用placement new:void* ptr = malloc(size); new(ptr) Type(args);
  • 所以,使用 malloc 是不好的做法?...谢谢@Joachim,我尝试了您的代码,现在它工作正常...
  • @user2869359 so, it is bad practice then to use malloc ? 在 C++ 中是的(不仅是不好的做法,而且如果你期望一些构造函数运行,那就错了)。在 C 中,没关系(但那是另一种语言)。
  • @user2869359 在这种情况下,这甚至不是坏习惯,这是完全错误的。如果您系统地使用new 而不是malloc,即使对于非常简单的情况,您也是安全的。
  • 好的,伙计们,...感谢您的指出。那我得再研究一下。干杯:)!
猜你喜欢
  • 2023-03-15
  • 2019-06-20
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-30
  • 1970-01-01
相关资源
最近更新 更多