繁体   English   中英

程序因信号 SIGABRT 终止,中止

[英]Program terminated with signal SIGABRT, Aborted

#include<stdio.h>
#include<stdlib.h>
int main(void)
{
    int **seqList, n, q;
    scanf("%d %d", &n, &q);
    seqList = (int**)malloc(n * sizeof(int*));
    int *l_sub_seq = (int*)calloc(n, sizeof(int));//length of subsequences of all sequences                                              
    int lastAnswer = 0;

    while(q--)
    {
        int type, x, y, i;
        scanf("%d %d %d", &type, &x, &y);
        i = (x^lastAnswer) % n;
        switch(type)
        {
            case 1:
            l_sub_seq[i]++;
            seqList[i] = (int*)realloc(seqList[i], sizeof(int)*l_sub_seq[i]);
            seqList[i][l_sub_seq[i] - 1] = y;
            break;

            case 2:
            lastAnswer = seqList[i][y%l_sub_seq[i]];
            printf("\n");
            break;
        }
    }
    for(int i = 0; i < n; i++)
        free(seqList[i]);
    free(seqList);

    for(int i = 0; i < n; i++)
        free(l_sub_seq);

    return 0;
}

编译器消息:

free(): 在 tcache 2 中检测到双空闲
从解决方案中读取符号...完成。
[新 LWP 335079]
[启用使用 libthread_db 的线程调试]
使用主机 libthread_db 库“/lib/x86_64-linux-gnu/libthread_db.so.1”。
核心是由`./Solution' 生成的。
程序以信号 SIGABRT 终止,中止。
#0 __GI_raise (sig=sig@entry=6) 在 ../sysdeps/unix/sysv/linux/raise.c:50

您的代码可能至少在一个位置调用未定义的行为,并且肯定会在另一个位置调用它。

realloc允许输入指针值是:

  1. NULL
  2. malloccallocrealloc返回的值

您对seqList初始分配:

seqList = (int**)malloc(n * sizeof(int*));

创建一个指向 int 的指针序列。 这些指针是不确定的(它们没有确定的值,可以是 NULL 或任何其他有效值),可以传递给realloc 因此,稍后在代码中执行此操作时:

seqList[i] = (int*)realloc(seqList[i], sizeof(int)*l_sub_seq[i]);
here ======================^^^^^^^^^^

您正在调用未定义的行为 您可以通过使用零填充calloc (最简单)或循环、memset 等方式确保初始数组内容为空填充来解决此问题。

稍后,在程序结束时,您执行以下操作:

for (int i = 0; i < n; i++)
    free(l_sub_seq);

那是胡说八道。 l_sub_seq被分配了:

int *l_sub_seq = (int*)calloc(n, sizeof(int));

它应该被释放一次,而不是在某个循环中,反复将相同的指针值一遍又一遍地传递给free

free(l_sub_seq); // no loop.

程序的其余部分是否“有效”由您决定,但终止问题的原因可能来自上述问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM