int main() {
struct lottery *array;
array = (struct lottery *)malloc(3000 * sizeof(struct lottery));
int opt, counter;
menu1();
scanf("%d", &opt);
if (opt == 1)
Load(array, &counter);
else
exit("0");
menu2();
counter--;
scanf("%d", &opt);
while (opt != 7) {
switch (opt) {
case 1:
Save(array);
break;
case 2:
Enterd(array, &counter);
printf("%d\n", counter);
break;
}
menu2();
scanf("%d", &opt);
}
return 0;
}
void Enterd(struct lottery *a, int *count) {
struct lottery *b;
int x;
(*count)++;
x = *count;
printf("Your new data will have an ID of %d\n",x);
a[x].aa = x;
b = (struct lottery *)realloc(a, x * sizeof(struct lottery));
if (b == NULL) {
printf("Memory could not be allocated for your new input.Program will now exit...\n");
exit("0");
}
a = b;
printf("What is the date of your new draw?\n");
scanf("%d/%d/%d", &a[x].date1.day, &a[x].date1.month, &a[x].date1.year);
printf("Now please insert the 5 non-joker numbers\n");
scanf("%d%d%d%d%d", &a[x].n1, &a[x].n2, &a[x].n3, &a[x].n4, &a[x].n5);
printf("What is the 'Joker' number of this draw?\n");
scanf("%d", &a[x].joker);
printf("Your input is now complete.");
}
我正在写一个关于一些彩票文件的保护。我在向彩票数组添加更多数据的函数中遇到这个问题。每当x
包含1989年,我的realloc
来电返回NULL
。我设置x
到 1985 年i
可以向数组添加 4 个以上的输入,但无论何时x
1989年,它依然回归NULL
。我的问题是:代码有问题还是我的内存仍然不足?
如果 realloc 返回 null,首先打印出您要求分配的内存量。如果是负数或者很大的数字,那就有问题了。如果它是一个合理的数量,并且您有一台还算不错的机器,那么您不太可能内存不足。所以 malloc() 系统肯定以某种方式被破坏了。要么您传递了一个无效的指针,要么您已经写到了块的末尾,可能是在程序的完全不相关的部分。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)