我有一个任务,我想实现一个动态增长的数组,但我似乎遇到了 realloc() 的一些问题。只要我没有真正到达 realloc() 部分,我的代码就可以工作,由于某种原因,只有一些特定的值被更改为不同的值。现在,如果我写/读越界,我希望行完全不同,但我似乎无法确定它。问题似乎在第 30-40 行之间。这是代码:
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 typedef struct __student {
6 unsigned long ID;
7 char fname[33];
8 char lname[33];
9 float grade;
10 } student;
11
12 void partA(FILE *fp) {
13
14 int i, r;
15 i = r = 0;
16 int N = 1000;
17 student **S;
18
19 S = malloc(sizeof(student *) * N);
20
21 // While EOF has not been reached
22 while(!feof(fp)){
23 // Allocate enough memory to hold each student struct
24 S[i] = malloc(sizeof(student));
25 // Get each line and, using a scanset, get corresponding
26 // data into respective struct fields
27 fscanf(fp, "%d %[^,], %s %f", &S[i]->ID, S[i]->lname, S[i]->fname, &S[i]->grade );
28 i++; // Get next line into a new struct
29
30 // If array size has been reached...
31 if(i == N){
32 N *= 2;
33 // Double it
34 S = realloc(S, sizeof(student) * N);
35 if(S == NULL) {
36 // realloc has failed
37 printf("Memory reallocation failed; Fatal error.");
38 break;
39 }
40 }
41 }
42 r = i-1;
43 // Output data
44 printf("Name\t\t\t\t\t\t\t\t [ID]\t\t:\tGrade\n");
45 printf("___________________________________________________________________________________________________\n");
46 for(i=0; i<r; i++){
47 printf("%-32s %-32s [%lu]\t:\t%.3f\n", S[i]->fname, S[i]->lname, S[i]->ID, S[i]->grade);
48 free(S[i]);
49 }
50 }
这是我正在使用的输入文件:
58205720 Broke, Jim 95
29571057 Crowe, John 88
12957206 Moore, Kim 22
59376027 Sarasvaki, Joe 79
49027650 Morrigan, Tracy 68
30773967 Trund, Geoffrey 99
34850470 Perry, Tracey 77
70209658 Oatnel, Skye 89
预期的输出如下(只要 N 很高,即大于实际行数并且不会导致调用 realloc() ,我就会得到):
Name [ID] : Grade
___________________________________________________________________________________________________
Jim Broke [58205720] : 95.000
John Crowe [29571057] : 88.000
Kim Moore [12957206] : 22.000
Joe Sarasvaki [59376027] : 79.000
Tracy Morrigan [49027650] : 68.000
Geoffrey Trund [30773967] : 99.000
Tracey Perry [34850470] : 77.000
Skye Oatnel [70209658] : 89.000
但是,如果我设置 N = 3,我会得到以下结果:
Name [ID] : Grade
___________________________________________________________________________________________________
Jim Broke [58205720] : 95.000
John Crowe [29571057] : 88.000
Kim Moore [12957206] : 22.000
Joe Sarasvaki [59376027] : 79.000
Tracy Morrigan [49027650] : 68.000
Geoffrey Trund [30773967] : 99.000
Tracey Perry [231963084454] : 77.000
Skye Oatnel [231998443642] : 89.000
我对正在发生的事情感到很茫然。我尝试使用 gdb 检查堆栈和本地值,但还没有多少运气。对于为什么 ID 是唯一被破坏的东西,我也很矛盾。是否有必要创建一个单独的函数来返回指向我通过 realloc() 获得的新空间的指针?我想让我的代码尽可能紧凑,所以我选择首先尝试这种方式,因为手册页似乎支持了我对 realloc() 工作方式的假设。谢谢你。