在第一条主线C上撞车
我在一个项目上犯了一个非常奇怪的错误,我正在编写代码。 下面是错误(与valgrind一起) 我会在我的同事电脑(实际上是MACOSX)上加上这一点,它工作得非常好 这是我们的(awfull)main,第70行是我们的.c文件中main的第一行:(仍然是WIP请原谅这里发生的一切^)在第一条主线C上撞车,c,crash,valgrind,main,C,Crash,Valgrind,Main,我在一个项目上犯了一个非常奇怪的错误,我正在编写代码。 下面是错误(与valgrind一起) 我会在我的同事电脑(实际上是MACOSX)上加上这一点,它工作得非常好 这是我们的(awfull)main,第70行是我们的.c文件中main的第一行:(仍然是WIP请原谅这里发生的一切^) intmain(intargc,char*argv[]){ printf(“derp\n”); int i=0; 字符行[MAXLINES][BUFSIZ]; 字符*路径[MAXLINES]; printf(“de
intmain(intargc,char*argv[]){
printf(“derp\n”);
int i=0;
字符行[MAXLINES][BUFSIZ];
字符*路径[MAXLINES];
printf(“derp\n”);
文件*fp=fopen(“./Images/125/DBtexte.txt”,“r”);
如果(fp==0){
fprintf(stderr,“无法打开DBtexte\n”);
出口(1);
}
printf(“derp\n”);
而(i 对于(size_t j=0;jAre)来说,行和路径对堆栈来说太大了吗?如果是这样,你可以尝试减少最大行和BUFSIZE
。或者,在堆上用malloc
分配数组。不是我编码的。我尝试了一些事情,更改了最大行值,但什么都没有发生(在代码中定义为5000以上)然后将BUFSIZE改为10,它成功了,我将与我的朋友一起查看并尝试找到解决方法,感谢您的帮助!:)快速解决方法:使路径
(可能还有行
)成为全局(或静态)变量。基本上,只需将其向上移动一点,在main之外。这样它就不会在堆栈上分配。这对行:char*temp2=(char*)malloc(1+strlen(folder)+strlen(temp));strcat(temp2,folder);
有几个问题。1)在C中,从malloc()强制转换返回值这只会导致调试和维护问题。2)始终检查malloc()返回的值(!=NULL),以确保操作成功。3)temp2
指向的内存包含垃圾,因此任何人都可以猜测NUL字节的位置。它可能超出分配内存的范围。使用:`strcpy:`(temp2,folder);由于对malloc()
进行了多次调用,而没有相应地调用free()
==1930== Memcheck, a memory error detector
==1930== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==1930== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==1930== Command: ./main
==1930==
==1930== Warning: client switching stacks? SP change: 0xfff000280 --> 0xffc8e6590
==1930== to suppress, use: --max-stackframe=41000176 or greater
==1930== Invalid write of size 8
==1930== at 0x401288: main (main.c:70)
==1930== Address 0xffc8e6588 is on thread 1's stack
==1930== in frame #0, created by main (main.c:69)
==1930==
==1930==
==1930== Process terminating with default action of signal 11 (SIGSEGV)
==1930== Access not within mapped region at address 0xFFC8E6588
==1930== at 0x401288: main (main.c:70)
==1930== If you believe this happened as a result of a stack
==1930== overflow in your program's main thread (unlikely but
==1930== possible), you can try to increase the size of the
==1930== main thread stack using the --main-stacksize= flag.
==1930== The main thread stack size used in this run was 8388608.
==1930==
==1930== Process terminating with default action of signal 11 (SIGSEGV)
==1930== Access not within mapped region at address 0xFFC8E6581
==1930== at 0x4A236C0: _vgnU_freeres (vg_preloaded.c:58)
==1930== If you believe this happened as a result of a stack
==1930== overflow in your program's main thread (unlikely but
==1930== possible), you can try to increase the size of the
==1930== main thread stack using the --main-stacksize= flag.
==1930== The main thread stack size used in this run was 8388608.
==1930==
==1930== HEAP SUMMARY:
==1930== in use at exit: 37 bytes in 1 blocks
==1930== total heap usage: 1 allocs, 0 frees, 37 bytes allocated
==1930==
==1930== LEAK SUMMARY:
==1930== definitely lost: 0 bytes in 0 blocks
==1930== indirectly lost: 0 bytes in 0 blocks
==1930== possibly lost: 0 bytes in 0 blocks
==1930== still reachable: 37 bytes in 1 blocks
==1930== suppressed: 0 bytes in 0 blocks
==1930== Reachable blocks (those to which a pointer was found) are not shown.
==1930== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==1930==
==1930== For counts of detected and suppressed errors, rerun with: -v
==1930== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Erreur de segmentation (segfault basically)
int main(int argc, char* argv[]) {
printf("derp\n");
int i = 0;
char lines[MAXLINES][BUFSIZ];
char* path[MAXLINES];
printf("derp\n");
FILE *fp = fopen("./Images/125/DBtexte.txt", "r");
if (fp == 0) {
fprintf(stderr, "failed to open DBtexte\n");
exit(1);
}
printf("derp\n");
while (i < MAXLINES && fgets(lines[i], sizeof(lines[0]), fp)) {
lines[i][strlen(lines[i])-1] = '\0';
i = i + 1;
}
i = 0;
printf("derp\n");
while(strcmp(lines[i],"\0"))
i++;
char *folder = "./Images/125/";
for (size_t j = 0; j<(size_t)i; j++) {
char *temp = lines[j];
char *temp2 = (char *) malloc(1 + strlen(folder)+ strlen(temp));
strcat(temp2,folder);
strcat(temp2,temp);
path[j] = temp2;
//printf("%s\n",path[j]);
}
fclose(fp);
int *visage = faces(i-1,i);
printf("\nTHE FACE IS A LIE\n");
/*if (argc < 2)
errx(2, "Usage:\n%s <path>", argv[0]);
SDL_Surface* image = load_image(argv[1]);
display_image(image);
ToGray(image);
display_image(image);
Binarize(image);
display_image(image); */
printf("ONSTART\n");
strongClassifier* yolo;
yolo = adaboost(path, visage, 124, 1, 20);
//int len;
//haarRecord* haarOutput;
//haarOutput = processImage(image, &len);
//display_haar(haarOutput, len);
//SDL_SaveBMP(image, "./ToGray.bmp");
return 0;
}