我使用malloc的代码遇到问题。直到一个小时前,它一直运行良好。导致此行
temp2 = (Temp*)malloc(sizeof(Temp));
我尝试删除(Temp *)以查看是否有帮助,但没有帮助。当我搜索错误消息
时untitled8(15926,0x1141ae5c0) malloc: Incorrect checksum for freed object 0x7fbff3c032e8: probably modified after being freed.
Corrupt value: 0x742e7962616c6c61
untitled8(15926,0x1141ae5c0) malloc: *** set a breakpoint in malloc_error_break to debug
Signal: SIGABRT (signal SIGABRT)
我从互联网上找到的答案与free()有关,但是在我释放任何分配的变量之前,它就出错了。
我有main.c和task1.c和task.2,还有3个头文件,下面的代码来自task1.c。从来都不想发布这样的冗长代码,但是由于我在其他部分使用了malloc,因此我也希望对其进行检查...对于任何不便之处,请提前阅读代码。
typedef struct histogramTemp {
char *words;
int count;
struct histogramTemp *next;
} HistogramTemp;
typedef struct temp{
char c;
struct temp *next;
} Temp;
HistogramTemp *task1(FILE *fp, char *fname){
char textfile, *string = NULL;
Temp *tempHead = NULL, *temp1, *temp2;
HistogramTemp *uniqueWordTemp = NULL, *headTemp, *uniqueWordTempHead = NULL;
if(fp == NULL){
printf("\n\n!! Error in opening file !!\n");
printf("Program will proceed with defult 'australia.txt' file. \n");
FILE *defultFp;
defultFp = fopen("/Users/katyang/CLionProjects/untitled8/australia.txt", "r");
fp = defultFp;
}
while((textfile = fgetc(fp))!=EOF){
// save temporary word as a separate char linked list 'Temp', and save it to 'string' as a whole word
if (isupper(textfile)>0) {
temp1 = (Temp*)malloc(sizeof(Temp));
temp1->c = textfile;
temp1->next = tempHead;
tempHead = temp1;
int i=0;
while(tempHead != NULL){
string = malloc(30*sizeof(char));
strcpy(&string[i],&tempHead->c);
i++;
tempHead = tempHead->next;
}
while ((textfile = fgetc(fp))!=EOF) {
if (isalpha(textfile)>0 && !(isupper(textfile))) {
temp2 = (Temp*)malloc(sizeof(Temp));
temp2->c = textfile;
temp2->next = tempHead;
tempHead = temp2;
while(tempHead != NULL){
strcpy(&string[i],&tempHead->c);
i++;
tempHead = tempHead->next;
}
}
// use 'string', make Histogram
if(isupper(textfile) || !isalpha(textfile)){
int flag=0;
int commonWordsFlag=0;
// check if the words are in the commonWords list
for (int j = 0; j < 122 ; j++) {
if (strcmp(string, commonwords[j])==0){
commonWordsFlag++;
break;
}
}
if((strlen(string)<3) || (commonWordsFlag == 1)){
break;
}
headTemp = uniqueWordTempHead;
// compare string to uniqueWordTemp
while (uniqueWordTempHead != NULL){
// increment count if the word is in Histogram
if(strcmp(uniqueWordTempHead->words, string)==0){
uniqueWordTempHead->count++;
flag++;
uniqueWordTempHead=uniqueWordTempHead->next;
}else{
uniqueWordTempHead=uniqueWordTempHead->next;
continue;
}
}
// create new node if the word is not in Histogram
if ((uniqueWordTempHead == NULL) && (flag == 0)){
uniqueWordTempHead = headTemp;
uniqueWordTemp = (HistogramTemp*)malloc(sizeof(HistogramTemp));
uniqueWordTemp->words = string;
uniqueWordTemp->count=1;
// insert in head
uniqueWordTemp ->next = uniqueWordTempHead;
uniqueWordTempHead = uniqueWordTemp;
}else{
uniqueWordTempHead = uniqueWordTemp;
}
break;
}
}
}
}
createNewFile(fname, uniqueWordTempHead);
free(string);
free(tempHead);
return(uniqueWordTempHead);
}
所以,我希望将数据保存在temp2中。似乎在调试时很奇怪。.调试时我没有问题,但是每次执行程序时,程序都以退出代码11结尾。
修改 我添加了main.c和task1()的头文件以获取更多信息。
main.c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#include "task1.h"
#include "task2.h"
int main() {
FILE *fp;
char *fname = malloc(sizeof(char));
printf("\n\n:::::::::::::: TASK 1 ::::::::::::::\n\nPlease Enter the Full Path of the file: \n");
scanf("%s", fname);
fp = fopen( fname , "r");
task1(fp, fname);
HistogramTemp *uniqueWordTempHead = task1(fp, fname);
task2(fp, fname);
free(fname);
free(uniqueWordTempHead);
return 0;
}
头文件
#ifndef UNTITLED8_TASK1_H
#define UNTITLED8_TASK1_H
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
typedef struct histogramTemp {
char *words;
int count;
struct histogramTemp *next;
} HistogramTemp;
typedef struct temp{
char c;
struct temp *next;
} Temp;
HistogramTemp *task1(FILE *fp, char *fname);
int countHistogram (HistogramTemp *head);
void printHistogram (HistogramTemp *head, FILE *fp);
void createNewFile(char *userFilename, HistogramTemp *head);
#endif //UNTITLED8_TASK1_H
答案 0 :(得分:1)
您不能像这样将“ char
”仅附加到“字符串”:
strcpy(&string[i],&tempHead->c);
strcpy()
需要一个指向“字符串”的第一个char
的指针作为第二个参数。在C中,“字符串”是char
的数组,其中至少一个char
等于'\0'
。
使用
string[i] = tempHead->c;
代替并终止string
string[i] = '\0';
也在这里
while(tempHead != NULL){
string = malloc(30*sizeof(char));
string
分配给每个迭代,覆盖先前迭代中接收的点。这会导致巨大的内存泄漏。
更多分配是循环的。
所以这个
while(tempHead != NULL){
string = malloc(30*sizeof(char));
strcpy(&string[i],&tempHead->c);
i++;
tempHead = tempHead->next;
}
会看起来像这样
string = malloc(30*sizeof(char));
i = 0;
while(tempHead != NULL && i < 29){ // one less to be able to store the '0' terminator
string[i] = tempHead->c;
i++;
tempHead = tempHead->next;
}
string[i] = '\0'; // store the '0' terminator