简体   繁体   English

分段错误(Malloc/Free in a Loop)

[英]Segmentation Fault (Malloc/Free in a Loop)

I've been revisiting the C language and am having trouble freeing up memory after use in my program:我一直在重温 C 语言,但在我的程序中使用后无法释放内存:

    int tvalue = 2;
    while (smult == 0) {
        int * tvaluearray = calloc(allnum, sizeof(int));    
        tvaluearray = frequencyArray(tvalue, allnum, tvaluearray);
        printf("tvalue = %d\n", tvalue);    
        //compare each index of the tvaluearray and the first array
        for (int j = 0; j < allnum; j++) {
//          printf("tvaluearray[%d]=%d >= firstarray[%d]=%d\n", j, tvaluearray[j], j, firstarray[j]);
            if (tvaluearray[j] < firstarray[j]) {
            //  printf("Found the false statement\n");
                break;
            }
            else if ( (j+1) == allnum ){
                smult = 1;
//              printf("Made it to else if! smult = %d\n", smult);
            }
        }
        free(tvaluearray);
        ++tvalue;
    }

The frequencyArray function is shown below: frequencyArray 函数如下所示:

int * frequencyArray (int target, int allnum, int targetarray[]) {
    int divisor = 2;

    for (int i = 0; i < allnum; i++)
        targetarray[i] = 0;
    //find the common factor frequency of the given number
    while (target > 1) {
        if (target % divisor == 0) {
            targetarray[divisor] += 1;
            target /= divisor;
        }
        else
            ++divisor;
    }


    return targetarray;
}

Having played around with this a bit, I've tried the following with different results:稍微玩弄了一下,我尝试了以下不同的结果:

1) removing the free of targetarray: 1)去除targetarray的空闲:

tvalue = 1306 --> segfault tvalue = 1306 --> 段错误

2) including the free(targetarray): 2)包括免费(目标数组):

tvalue = 29 free(): invalid next size (fast) Aborted (core dumped) tvalue = 29 free():下一个大小无效(快速)中止(核心转储)

3) including free(targetarray) AND allocating 4*sizeof(int) for the tvaluearray calloc as opposed to just int: 3) 包括 free(targetarray) 并为 tvaluearray calloc 分配 4*sizeof(int) 而不是仅分配 int:

tvalue = 31468 --> segfault tvalue = 31468 --> 段错误

The third test had me changing the allocated space for the array with varying results before my program runs into the segmentation fault error.第三个测试让我在我的程序遇到分段错误错误之前更改了数组的分配空间,结果各不相同。 This has me thinking that there's an issue with the way I'm allocating space, but I think it just might be a little bit beyond my current understanding.这让我认为我分配空间的方式存在问题,但我认为这可能有点超出我目前的理解。 Do any of y'all see where I may be going wrong?你们中的任何人都看到我可能会出错的地方吗?

In frequencyArray() function you have a loop:frequencyArray()函数中有一个循环:

while (target > 1) {
    if (target % divisor == 0) {
        targetarray[divisor] += 1;
        target /= divisor;
    }
    else
        ++divisor;
}

where divisor is used as index of your targetarray[] .其中divisor用作targetarray[]索引。 I can't see here any formal bounds for maximum value of divisor - it seems that it can grow over maximum allowed value (equal allnum - 1 ), so the memory outside targetarray[] can be overwritten resulting in memory corruption/segmentation fault.我在这里看不到divisor最大值的任何正式界限 - 它似乎可以超过最大允许值(等于allnum - 1 ),因此targetarray[]之外的内存可以被覆盖,导致内存损坏/分段错误。 You should check in each iteration that divisor < allnum .您应该检查divisor < allnum每次迭代。

Unfortunately, I don't know the context of your code, so can't propose any appropiate solution here.不幸的是,我不知道你的代码的上下文,所以不能在这里提出任何合适的解决方案。 Probably it should look like:大概应该是这样的:

while (target > 1) {
    if (target % divisor == 0) {

        // 'fuse' ;)
        if (divisor >= allnum) {
           // place here the code needed to solve the problem or break to exit from loop
        }
        // end of 'fuse'

        targetarray[divisor] += 1;
        target /= divisor;
    }
    else
        ++divisor;
}

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

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