realloc 中的无效指针
invalid pointer in realloc
这个错误以前从来没有出现过,我不明白为什么会这样。我正在创建一个 C 程序来创建一个数组,然后重新分配它以添加更多元素。我在同一个程序中应用了很多不必要的概念来浓缩一些知识。
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// ALWAYS when doing a definition of new elements in a arrays, NEVER use size as a parameter
// because if you assign one element as one, it will reset the value of size then reseting
// size < sizenew.
// Ex: Do not do:
//
// for (size; size < newsize; size++) scanf ("%hu", &size)
//
// the number you choose for scanf will affect the number of times the loop will do, because size
// is a parameter of while
int main()
{
_Bool decision;
int* array;
unsigned short size, newelements, oldsize, add, minus_oldsize, newsize, i, j;
printf("Hello! put the size of the initial int array!\n");
scanf("%hu", &size);
array = malloc(size * 4);
printf("Put the array elements one by one\n");
for (i = 0; i < size; i++) scanf("%d", array + i);
printf("This is your array:\n");
for (j = 0; j < size; j++) printf("%d\n", *(array + j));
printf("Do you want to add more elements? no(0) yes(1)\n");
scanf("%d", &decision);
if (decision != 1) return -1;
printf("How many elements you want to add?\n");
scanf("%d", &newelements);
newsize = size + newelements;
array = realloc(array, newsize * 4);
free(array);
return 0;
}
错误:
realloc(): invalid pointer
看来问题出在对 _Bool
类型对象的 scanf
调用
_Bool decision;
//...
scanf("%d", &decision);
遗憾的是,_Bool
类型的对象没有可以安全地用于 scanf
.
调用的转换说明符
相反,您可以使用 int
.
类型的对象
另一个问题是在此调用中使用了不正确的转换说明符
scanf("%d", &newelements);
你的意思好像是
scanf("%hu", &newelements);
这个错误以前从来没有出现过,我不明白为什么会这样。我正在创建一个 C 程序来创建一个数组,然后重新分配它以添加更多元素。我在同一个程序中应用了很多不必要的概念来浓缩一些知识。
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// ALWAYS when doing a definition of new elements in a arrays, NEVER use size as a parameter
// because if you assign one element as one, it will reset the value of size then reseting
// size < sizenew.
// Ex: Do not do:
//
// for (size; size < newsize; size++) scanf ("%hu", &size)
//
// the number you choose for scanf will affect the number of times the loop will do, because size
// is a parameter of while
int main()
{
_Bool decision;
int* array;
unsigned short size, newelements, oldsize, add, minus_oldsize, newsize, i, j;
printf("Hello! put the size of the initial int array!\n");
scanf("%hu", &size);
array = malloc(size * 4);
printf("Put the array elements one by one\n");
for (i = 0; i < size; i++) scanf("%d", array + i);
printf("This is your array:\n");
for (j = 0; j < size; j++) printf("%d\n", *(array + j));
printf("Do you want to add more elements? no(0) yes(1)\n");
scanf("%d", &decision);
if (decision != 1) return -1;
printf("How many elements you want to add?\n");
scanf("%d", &newelements);
newsize = size + newelements;
array = realloc(array, newsize * 4);
free(array);
return 0;
}
错误:
realloc(): invalid pointer
看来问题出在对 _Bool
scanf
调用
_Bool decision;
//...
scanf("%d", &decision);
遗憾的是,_Bool
类型的对象没有可以安全地用于 scanf
.
相反,您可以使用 int
.
另一个问题是在此调用中使用了不正确的转换说明符
scanf("%d", &newelements);
你的意思好像是
scanf("%hu", &newelements);