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.");
}
Run Code Online (Sandbox Code Playgroud)
我正在为一些彩票文件编写保护措施。我在向彩票阵列添加更多数据的函数中遇到此问题。每当x包含1989时,我的realloc电话就会返回NULL。我设置x为1985,i可以向数组添加4个以上的输入,但是只要x是1989,它仍然会返回NULL。我的问题是:代码是否有问题,或者我仍然用尽内存?
如果 realloc 返回 null,首先打印出您要求分配的内存量。如果它是一个负数或一个巨大的数量,那就是问题所在。如果这是一个合理的数量,并且您的机器还不错,那么您很可能没有内存。所以 malloc() 系统一定在某种程度上被破坏了。要么你传递了一个无效的指针,要么你写到了块的末尾,可能是在程序的一个完全不相关的部分。
| 归档时间: |
|
| 查看次数: |
1472 次 |
| 最近记录: |