我试着对不能用经典数字类型表示的数字进行质数分解。
我把那个大数字读成字符串,然后除以所有小于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]);
谢谢你的一切,我会为哪怕一点点线索而高兴!
最佳答案
问题是您在doDecomposition
函数中释放了divident。
因此,如果条件if(r == 0)
为真,则在main
中执行的下一个循环中,您将尝试在不属于自己的内存中写入。如果在循环中,您已经在程序结束时释放了它,为什么要释放它?
另一个问题,你为什么要这么做?阅读if(divident[0] == '0' && divident[1]=='\0')
手册,它更易读,更易于使用;)
最后一件事,当你使用malloc时,我认为设置strcmp
比设置int *i = malloc(sizeof(*i));
更好,因为如果你想更改i的类型,你必须用正确的类型更改每个分配。当你有一个小程序的时候这不是问题,但是有一个大的程序就更难了。