<分区>
我的要求是为我正在创建的 char *
变量进行动态内存分配,以便当我的应用程序中的 while
循环读取数据时,内存分配应该相应地增加,但是当我运行代码时会发生什么,我得到了错误,
*** 检测到 glibc *** ./rpp: realloc(): invalid next size: 0x0000000001376010 ***
我通过谷歌搜索 ( https://www.google.com/search?client=ubuntu&channel=fs&q=realloc%28%29%3A+invalid+next+size%3A+0x0000000002401010+&ie=utf-8&oe=utf-8#channel=fs&q=realloc%28%29:+invalid+next+size:+ ) 在 stackoverflow 中发现了很多关于这个主题的问题,但没有帮助。如果你们的专家能帮助我解决这个错误,我真的很感激,因为我现在对如何解决这个问题一无所知。在 id 之后给出错误的函数。
int execScanNetowrk(){
FILE *fp;
int status;
char path[1035];
char *pathFull;
pathFull = (char*)malloc(sizeof(path));
/* Open the command for reading. */
fp = popen("nmap -sP -oG - 192.168.1.0/24", "r");
if (fp == NULL) {
printf("Failed to run command\n" );
exit;
}
/* Read the output a line at a time - output it. path stores each line of data per iteration*/
while (fgets(path, sizeof(path)-1, fp) != NULL) {
pathFull = (char*)realloc(pathFull, sizeof(path));//increase memory allocation
strcat(pathFull,path);//concatenate char * with the read data
}
printf("%s\n", a);
/* close */
pclose(fp);
free(a);
return 0;
}
非常感谢:)