首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C程序返回0xc0000005状态

C程序返回0xc0000005状态
EN

Stack Overflow用户
提问于 2014-06-12 20:06:37
回答 1查看 17.1K关注 0票数 0

我正在尝试编写一个将二叉树转换为链表的C程序。下面是这些对结构的声明:

代码语言:javascript
运行
复制
struct treeNode {
    double data;
    struct treeNode* leftP;
    struct treeNode* rightP;
};

struct listNode{
    double data;
    struct listNode* nextP;
};

算法是有效的,但我希望在程序执行结束时出现内存错误(进程返回0xC0000005)。在对这段代码进行了一些搜索之后,我猜它来自于一个糟糕的指针管理,但我找不到我的错误。下面是我的主要函数:

代码语言:javascript
运行
复制
int main()
{
    /*Creation of the binary tree*/
    struct treeNode tree[10];
    int i;
    for(i=0;i<10;i++)
        tree[i].data = (float)i;
    tree[0].leftP = &(tree[1]);
    tree[0].rightP = &(tree[2]);
    //....... (creation of the tree)
    tree[9].rightP = 0;

    //Conversion
    void** elem = malloc(sizeof(void**));
    *elem = (void*)(&tree);
    tree2list(elem);
    struct listNode* list = (struct listNode*)(*elem);
    printList(list);
    printf("end1");
    //free(elem);
    printf("end2\n");
    return 0;
}

如果我注释空行,程序会到达end2;如果我试图释放变量elem,程序会到达end1。下面是tree2list函数:

代码语言:javascript
运行
复制
void tree2list(void** node)
{
    struct treeNode* tree = (struct treeNode*)(*node); //tree to convert
    int size = treeSize(tree);
    struct listNode* list = malloc(size*sizeof(struct listNode*)); //Creation of a list with same number of nodes than in the tree
    *node = (void*) list;
    struct listNode* currentListNode = list;
    struct treeNode* currentTreeNode = tree;
    struct treeNode* nextNode;
    struct listNode* old = 0;
    struct treeNode* stack = 0; //Stack composed of treeNode linked by their leftP pointer
    while(currentTreeNode)
    {
        if(currentTreeNode->leftP) //if left branch exists, add the current node to the stack and explore this left branch
        {
            nextNode = currentTreeNode->leftP;
            stackPush(&stack, currentTreeNode);
            currentTreeNode = nextNode;
        }
        else //if left branch doesn't exist, add the currentNode to the list
        {
            currentListNode->data = currentTreeNode->data;
            currentListNode->nextP = 0;
            if(old)
                old->nextP = currentListNode;
            old = currentListNode++;
            if(currentTreeNode->rightP)
                currentTreeNode = currentTreeNode->rightP;
            else
                currentTreeNode = stackPop(&stack);
        }
    }
}

像stackPush和stackPop这样的其他函数的这个函数似乎工作得很好。有没有人看到错误代码的源代码?

谢谢

EN

Stack Overflow用户

发布于 2014-06-12 20:28:36

代码语言:javascript
运行
复制
void** elem = malloc(sizeof(void**));

这没有任何意义。想想你在这里真正想要做的是什么。同样,这也没有任何意义:

代码语言:javascript
运行
复制
struct listNode* list = malloc(size*sizeof(struct listNode*));

在第一个malloc中,您可能需要分配一个指针数组,然后用指针指向它们。你分配了一个单一的指针到指针。如果这就是我们的意图,为什么一开始就动态分配呢?

在第二个malloc中,您可能需要分配一个结构数组。您分配了一个指向结构的指针数组。

考虑properly allocating a 2D array,而不是分配一个遍及整个堆的指针到指针查找表。

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24183959

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档