gpt4 book ai didi

C 数组被覆盖/删除?很困惑

转载 作者:行者123 更新时间:2023-12-02 01:44:36 27 4
gpt4 key购买 nike

我正在用 C 开发一个项目,它工作得很好,除了一个函数似乎正在覆盖我的数组并写入奇怪的数字,例如 1970802352,它保持文件中单词出现的计数

这是我的头文件:

#ifndef LIST_H
#define LIST_H
struct Node_{
char* word;
//array holding names of files word occurs in
char **filesIn;
int numFilesIn;
//array holding count of how many times word occured in file
int* occursIn;
struct Node_ *next;
int isHead;
};

typedef struct Node_ Node;

int insert(char *wordToAdd, char *File);

int addOccur(Node *addedTo, char *File);

Node *createNode(char *wordToAdd, char *File);

void destroyNodes();

#endif

这是不断覆盖数组的函数:

Node *head;
int insert(char *wordToAdd, char *File){
if(head == NULL){
Node *new;
new = createNode(wordToAdd, File);
new->isHead = 1;
head = new;
return 0;
}
else{
Node *trace;
trace = head;
char *traceWord;
int wordSize;
wordSize = strlen(trace->word);
traceWord = (char*) malloc(wordSize + 1);
strcpy(traceWord, trace->word);
int a =strcmp(wordToAdd, traceWord);
free(traceWord);
if(a == 0){
int b = addOccur(trace, File);
//printf("addOccur returned %d\n", b);
return 0;
}
if(a < 0){
Node *Insert = createNode(wordToAdd, File);
trace->isHead = 0;
Insert->isHead = 1;
Insert->next = trace;
head = Insert;
return 0;
}
else{


Node *backTrace;
backTrace = head;

while(trace->next != NULL){
trace = trace->next;
traceWord = trace->word;
a = strcmp(wordToAdd, traceWord);
if(a < 0){
Node* Insert = createNode(wordToAdd, File);
Insert->next = trace;
backTrace->next = Insert;
return 0;
}
if(a == 0){
addOccur(trace, File);
//free(wordToAdd);
return 0;
}
if(a > 0){
backTrace = trace;
continue;
}
}
Node *Insert = createNode(wordToAdd, File);
trace->next = Insert;
return 0;
}
}
return 1;
}

其他函数有:

Node* createNode(char *wordToAdd, char *File){
Node *new;
new = (Node*)malloc(sizeof(Node));
memset(new, 0, sizeof(Node));
new->word = wordToAdd;
char **newArray;
newArray = (char**)malloc(sizeof(char*));
newArray[0] = File;
new->filesIn = newArray;
int a[1];
a[0] = 1;
new->occursIn = a;
//new->occursIn[0] = 1;
new->numFilesIn = 1;
return new;
}

int addOccur(Node *addedTo, char *File){

char **fileList = addedTo->filesIn;
char *fileCheck;
int i = 0;
int fileNums = addedTo->numFilesIn;
for(i = 0; i < fileNums; i++){
fileCheck = fileList[i];
if(strcmp(fileCheck, File) == 0){
int *add1;
add1 = addedTo->occursIn;
int j = add1[i];
j++;
add1[i] = j;
return 0;
}
}

int numberOfFilesIn;
numberOfFilesIn = addedTo->numFilesIn;
char **newList = (char**)malloc(sizeof(char*) * numberOfFilesIn + sizeof(char*));
i = 0;
char *dest;
char *src;
for(i = 0; i < numberOfFilesIn; i++){
src = fileList[i];
int len;
len = strlen(src);
dest = (char*)malloc(sizeof(char) * (len + 1));
strcpy(dest, src);
newList[i] = dest;
}
int len2;
len2 = strlen(File);
newList[i] = File;
free(fileList);
int r = addedTo->numFilesIn;
r++;
addedTo->numFilesIn = r;
addedTo->filesIn = newList;
i = 0;
int *g;
g = addedTo->occursIn;
int count2;
count2 = addedTo->numFilesIn;
count2++;
int a[count2];
for(i = 0; i < count2 -1; i++){
a[i] = g[i];
}
a[count2 - 1] = 1;
return 0;
}

当转到 gdb 时,我注意到

的值

head->occursIn[0]

行后的变化

wordSize = strlen(trace->word);

我不知道为什么。

最佳答案

在您的 CreateNode() 函数中,您没有为 occursIn 数组分配存储空间。您只需在函数内声明一个本地数组,然后分配 occursIn 指针:

int a[1];
a[0] = 1;
new->occursIn = a;

数组 a[1] 在 createNode 函数返回时消失,因此此时您的 occursIn 指针指向一个可能被覆盖的值。

即使在 createNode 中正确分配了存储,您已经为数组设置了固定大小,但您的整个策略取决于该数组对每个文件都有一个元素;在 addOccurs 中,您无需为新文件分配更大的新数组。

您可能需要重新评估您的策略并改用列表而不是数组。

关于C 数组被覆盖/删除?很困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26445689/

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