【问题标题】:Exitcode 139 in CC中的退出代码139
【发布时间】:2022-07-05 20:03:01
【问题描述】:

我想在我的 LinkedList 中添加一个新元素。它可以添加在中间或末尾。 这是代码的一部分,我的调试器显示错误:

//...
        linkedElement * newElem;
        newElem->next = prevElement->next; //This line creates the error      
//...

错误信息是:

进程以退出代码 139 结束(被信号 11:SIGSEGV 中断)

我在某处读到,这意味着我尝试访问不存在的东西。但我看不出我做错了什么。

注意:我使用的是 MacOS

【问题讨论】:

  • 您需要学习如何使用调试器在受控环境中运行代码并检查变量的状态...

标签: c linked-list runtime-error exit-code


【解决方案1】:
//...
        linkedElement * newElem;
        newElem->next = prevElement->next; //This line creates the error      
//...

您首先使newElem 成为一个不指向任何有效位置的指针。

然后使用该“无处有效”将其 next 字段设置为某个值。

你需要分配内存(或者使用一些之前分配的对象)

//...
    linkedElement * newElem;
    newElem = malloc(sizeof *newElem);
    if (newElem == NULL) exit(EXIT_FAILURE);
    newElem->next = prevElement->next; //This line creates the error      
//...

【讨论】:

    猜你喜欢
    • 2021-03-07
    • 1970-01-01
    • 2017-01-20
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    相关资源
    最近更新 更多