【问题标题】:malloc() call in function - segmentation fault: 11malloc() 函数调用 - 分段错误:11
【发布时间】:2016-01-13 11:13:27
【问题描述】:

我是一名 C 初学者,刚刚尝试实现一个 makeRandomTree() 函数来随机创建一棵二叉树。

虽然有时编译成功(并非总是如此!)但程序在运行时退出并出现错误“Segmentation fault: 11”。我认为错误位于 makeRandomNode() 函数中,其中包含 malloc() 的动态内存分配。不幸的是,我还没有找到错误。

希望有人能帮助我=)

这里是源代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define NUMBER_OF_NODES 10
#define RANDOM_NUMBER_MAX 99

struct node {
    int nodeValue;
    struct node *parentNode;
    struct node *leftChild;
    struct node *rightChild;
};

typedef struct node node;

node* makeRandomNode(unsigned int *randomSeed) {
    //random number in the range [0;RANDOM_NUMBER_MAX]
    int random = (rand_r(randomSeed) % (RANDOM_NUMBER_MAX + 1));

    node *newNode = (node *) malloc(sizeof(node));

    newNode->nodeValue = random;
    newNode->parentNode = NULL;
    newNode->leftChild = NULL;
    newNode->rightChild = NULL;

    return newNode;
}

void insertNode(node *root, node *newNode) {
    if (newNode->nodeValue <= root->nodeValue) {
        if (root->leftChild == NULL) {
            root->leftChild = newNode;
            newNode->parentNode = root;
        }
        else {
            insertNode(root->leftChild, newNode);
        }
    }
    else {
        if (root->rightChild == NULL) {
            root->rightChild = newNode;
            newNode->parentNode = root;
        }
        else {
            insertNode(root->rightChild, newNode);
        }
    }
}

node* makeRandomTree(int numberOfNodes) {
    int i;
    unsigned int randomSeed;
    node *root;
    node *newNode;

    randomSeed = time(NULL);
    root = makeRandomNode(&randomSeed);

    for (i = 1; i < numberOfNodes; i++) {
        newNode = makeRandomNode(&randomSeed);  
        insertNode(root, newNode);
    }

    return root;
}

int main(int argc, char *argv[]) {
    node *tree = makeRandomTree(NUMBER_OF_NODES);

    printf("A random-binary-tree with %i nodes was created successfully!!!\n", NUMBER_OF_NODES);
    printf("'NodeValue' of root is %i\n", tree->nodeValue);
    printf("'NodeValue' of left child of root is %i\n", tree->leftChild->nodeValue);
    printf("'NodeValue' of right child of root is %i\n", tree->rightChild->nodeValue);
    printf("etc.\n");
}

【问题讨论】:

  • 欢迎来到 Stack Overflow!你介意创建一个MCVE吗?
  • 另外,学习使用像 gdb 这样的调试器,并逐步检查代码以查明错误指令。
  • 了解如何使用调试器。当您在调试器中运行时,它将停在崩溃的位置,并让您检查(并向上走)函数调用堆栈,并让您检查相关变量的值。至少请编辑您的问题,向我们展示您的代码中实际发生崩溃的位置。
  • @PelZa 我不会专注于 makeRandomTree(),对于 random 数字,您可能有 unexpected 序列...
  • 以上所有+请不要投malloc&co的回报

标签: c segmentation-fault malloc


【解决方案1】:

一般来说,树处理代码看起来没问题,但这可能是个问题:

printf("'NodeValue' of left child of root is %i\n", tree->leftChild->nodeValue);
printf("'NodeValue' of right child of root is %i\n", tree->rightChild->nodeValue);

如果碰巧这棵树没有左子或右子,您尝试取消引用NULL-指针。 由if (tree-&gt;leftChild) ...保护它

【讨论】:

  • 是的,通过 Valgrind 运行确认。现在快速,所有九个子节点值小于或大于根值的机会是多少。 ;)
  • 重新发布到 math.stackexchange.com ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-13
  • 2019-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-30
相关资源
最近更新 更多