gpt4 book ai didi

c - Malloc 信号 : SIGABRT (signal SIGABRT) problem

转载 作者:行者123 更新时间:2023-11-30 14:39:14 26 4
gpt4 key购买 nike

我的代码在使用 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() 有关,但在我释放任何 malloced 变量之前它出错了。

我有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

最佳答案

您不能仅将 char 附加到“字符串”,如下所示:

    strcpy(&string[i],&tempHead->c);

strcpy() 需要一个指向“字符串”的第一个 char 的指针作为第二个参数。在 C 中,“字符串”是一个由 char 组成的数组,其中至少有一个 char 等于 '\0' >.

使用

    string[i] = tempHead->c;

而是通过执行以下操作来终止字符串

  string[i] = '\0';

也在这里

          while(tempHead != NULL){
string = malloc(30*sizeof(char));

string get allocate to,每次迭代都会覆盖上一次迭代中收到的点。这会导致巨大的内存泄漏。

更多的循环外分配。

所以这个

        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

关于c - Malloc 信号 : SIGABRT (signal SIGABRT) problem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56204752/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com