在valgrind中写入/读取大小为1无效,未找到答案

时间:2016-11-07 07:15:59

标签: c linux memory-management valgrind

我试图对经典数字类型无法表示的数字进行素数分解。 我把这个大数字读成字符串,然后我将它除以小于10 ^ 6(预先生成)的所有素数。在进程中我使用动态分配内存。完成我的代码后,我检查了valgrind输出,发现了一些似乎是由同一件事引起的错误,我只是看不出我的代码有什么问题。我做了一项研究,虽然很多人得到了同样的错误,但解决方案并不适用于我的问题。 我会很满意每一条建议,因为我现在只是绝望了。

这是我的代码(没有对我的问题不重要的函数):

long* primeArray;
long endDiv = 101;
long r;
long length;
char *division(char dividend[],long divisor){
    char *multiplier = malloc(sizeof(char*)*endDiv);
    long temp=0;
    int i=0;
    int j=0;
    while(dividend[i]){  
         temp = temp*10 + (dividend[i] - 48);
         if(temp<divisor){
             multiplier[j++] = 48;
         }
         else{
             multiplier[j++] = (temp / divisor) + 48;
             temp = temp % divisor;
         }
         i++;
    }
    multiplier[j] = '\0';
    length = j;
    r = temp;
    return multiplier;
}
void doDecomposition(char* divident){
    long* primeDec = (long*)malloc(endDiv* sizeof(long*));
    int counter = 0;
    char * multiplier;  
    for(int i = 0;i<endPrime-1;i++){
            multiplier = division(divident, primeArray[i]);
            if(r == 0){
                free(divident);                 
                divident = multiplier;
                primeDec[counter] = primeArray[i];
                i = -1;
                counter++;
            }else{
                int f = 0;
                for(int a = length-1;a>0;a--){
                    if(multiplier[a] != '0'){
                        f = 1;
                        break;
                    }
                }
                free(multiplier);
                if(f==0){
                    break;
                }
            }   
    }
    printCount(primeDec, counter);
    free(primeDec);
    free(divident);
}
int main(int argc, char *argv[])
{
    char* divident = malloc(endDiv*sizeof(char*));
    genPrime(1000000);
    if(divident == NULL){
        exit(666);
    }
    while(fscanf(stdin, "%s", divident) == 1){  
        if(divident[0] == '0' && divident[1]=='\0'){
            exit(0);
        }
        else if(divident[0] == '1' && divident[1]=='\0'){
            fprintf(stdout, "Prime decomposition of 1 is:\n1\n");
        }
        else{
            checkDivident(divident);
            fprintf(stdout,"Prime decomposition of %s is:\n", divident);
            doDecomposition(divident);
        }   
    }
    free(divident); 
  return 0;
}

这是我的valgrind输出:

==17523== Invalid write of size 1
==17523==    at 0x51A0695: _IO_vfscanf (vfscanf.c:1107)
==17523==    by 0x51AE8E8: __isoc99_fscanf (isoc99_fscanf.c:34)
==17523==    by 0x40126F: main (main.c:185)
==17523==  Address 0x550c040 is 0 bytes inside a block of size 808 free'd
==17523==    at 0x4C2EDEB: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==17523==    by 0x401128: doDecomposition (main.c:161)
==17523==    by 0x401252: main (main.c:195)
==17523==  Block was alloc'd at
==17523==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==17523==    by 0x400C8A: division (main.c:73)
==17523==    by 0x401080: doDecomposition (main.c:145)
==17523==    by 0x401252: main (main.c:195)
==17523== 
==17523== Invalid write of size 1
==17523==    at 0x51A0752: _IO_vfscanf (vfscanf.c:1192)
==17523==    by 0x51AE8E8: __isoc99_fscanf (isoc99_fscanf.c:34)
==17523==    by 0x40126F: main (main.c:185)
==17523==  Address 0x550c041 is 1 bytes inside a block of size 808 free'd
==17523==    at 0x4C2EDEB: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==17523==    by 0x401128: doDecomposition (main.c:161)
==17523==    by 0x401252: main (main.c:195)
==17523==  Block was alloc'd at
==17523==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==17523==    by 0x400C8A: division (main.c:73)
==17523==    by 0x401080: doDecomposition (main.c:145)
==17523==    by 0x401252: main (main.c:195)
==17523== 
==17523== Invalid read of size 1
==17523==    at 0x4011C4: main (main.c:186)
==17523==  Address 0x550c040 is 0 bytes inside a block of size 808 free'd
==17523==    at 0x4C2EDEB: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==17523==    by 0x401128: doDecomposition (main.c:161)
==17523==    by 0x401252: main (main.c:195)
==17523==  Block was alloc'd at
==17523==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==17523==    by 0x400C8A: division (main.c:73)
==17523==    by 0x401080: doDecomposition (main.c:145)
==17523==    by 0x401252: main (main.c:195)
==17523== 
==17523== Invalid read of size 1
==17523==    at 0x4011D3: main (main.c:186)
==17523==  Address 0x550c041 is 1 bytes inside a block of size 808 free'd
==17523==    at 0x4C2EDEB: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==17523==    by 0x401128: doDecomposition (main.c:161)
==17523==    by 0x401252: main (main.c:195)
==17523==  Block was alloc'd at
==17523==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==17523==    by 0x400C8A: division (main.c:73)
==17523==    by 0x401080: doDecomposition (main.c:145)
==17523==    by 0x401252: main (main.c:195)
==17523== 
==17523== 
==17523== HEAP SUMMARY:
==17523==     in use at exit: 627,992 bytes in 1 blocks
==17523==   total heap usage: 1,252,545 allocs, 1,252,544 frees, 1,504,746,312 bytes allocated
==17523== 
==17523== LEAK SUMMARY:
==17523==    definitely lost: 0 bytes in 0 blocks
==17523==    indirectly lost: 0 bytes in 0 blocks
==17523==      possibly lost: 0 bytes in 0 blocks
==17523==    still reachable: 627,992 bytes in 1 blocks
==17523==         suppressed: 0 bytes in 0 blocks
==17523== Reachable blocks (those to which a pointer was found) are not shown.
==17523== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==17523== 
==17523== For counts of detected and suppressed errors, rerun with: -v
==17523== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 0 from 0)

行号信息:

185: while(fscanf(stdin, "%s", divident) == 1){ 
161: free(multiplier);
195: doDecomposition(divident);
73: char *multiplier = malloc(sizeof(char*)*endDiv);
145: multiplier = division(divident, primeArray[i]);

感谢所有事情,我会很高兴得到最轻微的线索!

1 个答案:

答案 0 :(得分:3)

问题是你在doDecomposition函数中释放了你的divident。 因此,如果条件if(r == 0)为真,则在main中的下一个循环中,您尝试写入您不拥有的内存。为什么你想要在你的循环中释放,你已经在程序结束时释放了它?

另一个问题,你为什么要做if(divident[0] == '0' && divident[1]=='\0')?阅读strcmp人,它更易读,易于使用;)

最后一点,当你使用malloc时,我认为最好使int *i = malloc(sizeof(*i));int *i = malloc(sizeof(int*));更好,因为如果你想改变i的类型,你必须用正确的类型改变每个分配。当你有一个小程序时,这不是问题,但是如果有一个大程序则更难。