我的代码在使用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发布于 2019-05-19 06:24:04
您不能只将一个char附加到这样的“字符串”:
strcpy(&string[i],&tempHead->c);strcpy()期望一个指向"string“的第一个char的指针作为第二个参数。在C中,”string“是一个数组 of 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' terminatorhttps://stackoverflow.com/questions/56204752
复制相似问题