好的,所以我正在读取一个 txt 文件并将每个项目按新行存储到一个字符串数组中。唯一的问题是,每当它快要填满时,我需要将这个数组的大小调整 5。
输入看起来像这样,除了大很多
CSE 1104
CSE 1105
CSE 1310
CSE 2320
IE 2308
PHYS 1443
MATH 1426
这是我的代码
void printFileError(char *s) {
printf("unable to open %s\n", s);
exit(1);
}
void reallocTaken(char **taken, int *size, int newSize) {
(*size) = newSize;
taken = realloc(taken, (*size) * sizeof(char *));
printf("Realocateding---- \n");
}
int main(int argc, char *argv[]) {
char **coursesTaken;
int coursesTakenSize = 5;
int i;
char *filePlan, *fileMyCourses;
FILE *fp;
/* Open the file of completed courses */
if((fp = fopen(argv[2], "r")) != NULL) {
char buffer[255];
int lineCount = 0;
coursesTaken = malloc(sizeof(char *) * coursesTakenSize);
while(fgets(buffer, sizeof(buffer), fp) != NULL) {
char token[] = "\n";
char *split = strtok(buffer, token);
while(split != NULL) {
/* Check if array is full */
if((lineCount + 1) == coursesTakenSize) {
printf("Needs to allocate \n");
/* Realoc Memory */
reallocTaken(coursesTaken, &coursesTakenSize, coursesTakenSize + 5);
}
coursesTaken[lineCount] = malloc((sizeof(split) + 1) * sizeof(char));
strcpy(coursesTaken[lineCount], split);
printf("%s\n", split);
lineCount++;
split = strtok(NULL, token);
}
}
/* Cut out exessive memory */
reallocTaken(coursesTaken, &coursesTakenSize, lineCount);
printf("Line Count: %d\n", lineCount);
printf("Size: %d\n", coursesTakenSize);
printf("Final Size: %lu\n", sizeof(coursesTaken));
fclose(fp);
} else {
printFileError(argv[2]);
}
for(i = 0; i < coursesTakenSize; i++) {
printf("%d:\t%s\n", i+1, coursesTaken[i]);
}
return 0;
}
这里的计划是 首先分配 5 个内存插槽,当行数等于大小时,再向其添加 5 个插槽。文件完成后,将整个文件的大小调整为行数。同时,分配数组的每个索引,用文本文件中的数据填充它。
代码成功重新分配前两个 block ,然后给出错误
a.out(9410,0x7fff7f20a300) malloc: *** error for object 0x7fcb9b404bb0: pointer being realloc'd was not allocated
这里发生了
CSE 1104
CSE 1105
CSE 1310
CSE 2320
Needs to allocate
Realocateding----
IE 2308
PHYS 1443
MATH 1426
MATH 1302
MATH 1323
Needs to allocate
我注意到通过评论 coursesTaken[lineCount] = malloc((sizeof(split) + 1) * sizeof(char))
程序完全运行直到打印每个
我不太确定这里发生了什么。
最佳答案
您混淆了 sizeof
和 ,它可以给出单个元素、结构或已知大小的数组 的大小>strlen
给出空终止字符串的长度。
coursesTaken[lineCount] = malloc((sizeof(split) + 1) * sizeof(char))
这行确实是罪魁祸首。在下一行中,您将 split
复制到其中,您需要它可以包含 split 中的所有字符以及终止空值。
但由于 split
是一个 char *
,sizeof(split)
是指针的大小 (例如,在 32 位系统上为 4,在 64 位系统上为 8)。
你应该写这一行:
coursesTaken[lineCount] = malloc((strlen(split) + 1) * sizeof(char));
关于创建一个动态字符串数组,该数组将在填满时缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30019709/