简体   繁体   English

C-收到SIGABRT错误,对我来说没有任何意义

[英]C - Getting a SIGABRT error, does not make any sense for me

So, my program should read an array of unknown size of positive integers in the format {1,2,3,4,5}, process it into an array, and then start reading pairs of integers a and b and find the least common multiple of a to (b-1)th elements of that array. 因此,我的程序应读取格式为{1,2,3,4,5}的未知大小的正整数数组,将其处理为数组,然后开始读取整数对a和b并找到最小公对数该数组的第a到第(b-1)个元素的倍数。 Here's my current solution: 这是我当前的解决方案:

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

int gcf(int a, int b) // greatest common divisor
{
    while (a*b!=0)
    {
        if (a<b)
            b=b-a;
        else
            if (b<a)
                a=a-b;
        else
            if (a==b)
                return a;
    }
    return 0;
}

int lcm(int a, int b) // least common multiplier
{
    return a*b/gcf(a,b);
}

int main()
{
    int a,bufUsed=0,bufCurr=0,i,b,signal=1,curlcm;
    char c;
    int* tmp;
    int* array;
    c=getchar();
    if (c!='{')
    {
        printf ("err0");
        return 0;
    }
    while((scanf("%d",&a))==1)
    {
        if (signal==0) // checking for the comma
        {
            printf("err1");
            return 0;
        }
        signal=0;
        printf("%d ",a); // displaying current values, used just for debugging
        if (bufUsed == bufCurr) //resizing the current array 
        {
           bufCurr += 20;
           tmp = (int*)realloc(array, bufCurr); // the line that causes trouble
           if (!tmp)
               printf("err2");
           array = tmp;
        }
        array[bufUsed] = a;
        bufUsed++;
        if (scanf(" %c",&c)==1) // checking for commas or closing bracket
        {
            if (c==',')
            {
                signal=1;
                continue;
            }
            if (c=='}')
                break;
            else
            {
                printf("err3");
                return 0;
            }
        }
        else
        {
            printf("err4");
            return 0;
        }
    }
    while ((scanf("%d %d",&a,&b))==2) // the second part, finding the LCM
    {
        curlcm=lcm(array[a],array[a+1]);
        for (i=2;i<b-a;i++)
        {
            curlcm=lcm(curlcm,array[a+i]);
        }
        printf("%d\n",curlcm);
    }
    return 0;
}

The line 线

tmp = (int*)realloc(array, bufCurr); tmp =(int *)realloc(array,bufCurr);

seems to be causing the SIGABRT, according to gdb, however, if I remove the second part of the program (finding the LCM while cycle), it works just fine. 根据gdb的说法,这似乎是导致SIGABRT的原因,但是,如果我删除程序的第二部分(在循环时查找LCM),则工作正常。 I've tried checking how the second part works with a defined array in the second program: 我尝试检查第二部分如何在第二个程序中与定义的数组一起工作:

int main()
{
    int a,i,b,curlcm;
    int array[10];
    for (i=0;i<10;i++)
        array[i]=i+1;      
    scanf("%d %d",&a,&b);
    curlcm=lcm(array[a],array[a+1]);
    for (i=2;i<b-a;i++)
    {
        curlcm=lcm(curlcm,array[a+i]);
    }
    printf("%d",curlcm);
}

and it runs just fine as well. 它也运行得很好。

So how come that combining these two causes the SIGABRT? 那么,将这两个因素结合在一起会导致SIGABRT吗? I've tried checking if accessing the array causes the problem, however, this seems not to be the case as well. 我已经尝试检查访问数组是否会导致问题,但是,似乎并非如此。 Thanks in advance for any suggestions. 在此先感谢您的任何建议。

As per the manual, the function 按照手册,功能

void *realloc(void *ptr, size_t size);

The realloc() function changes the size of the memory block pointed to by ptr to size bytes. realloc()函数将ptr指向的内存块的大小更改为大小字节。 The contents will be unchanged in the range from the start of the region up to the minimum of the old and new sizes. 从区域开始到新旧大小的最小值之间的内容将保持不变。 If the new size is larger than the old size, the added memory will not be initialized. 如果新的大小大于旧的大小,则不会初始化添加的内存。 If ptr is NULL , then the call is equivalent to malloc(size) 如果ptr为NULL ,则调用等效于malloc(size)

In the program, we have 在程序中,我们有

int* array;
...
tmp = (int*)realloc(array, bufCurr);

calling realloc the first time, while the array pointer is not initialized. array指针未初始化时,第一次调用realloc

The memory manager try to read some internal data based on that random address (like the size of the segment vs the requested size) which leads to undefined behavior (crash in your case). 内存管理器尝试根据该随机地址(例如段的大小与请求的大小)读取一些内部数据,这会导致未定义的行为(在您的情况下崩溃)。

As suggested, a simple solution consists of setting initially array to NULL , so that realloc does a simple malloc (since its ptr address is NULL ). 如建议的那样,一个简单的解决方案包括将初始array设置为NULL ,以便realloc进行简单的malloc (因为其ptr地址为NULL )。

int *array = NULL;

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

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