[英]Dynamic array using malloc and realloc?
I'm trying to collect input integers one by one. 我正在尝试一一收集输入整数。 My array starts with size 1 and I want to expand it by 1, with every input collected (is this a smart thing to do?)
我的数组从大小1开始,我想在收集到的每个输入中将其扩展为1(这是一件很聪明的事情吗?)
Anyway, this is the code I managed to come up with, but it's not working as intended. 无论如何,这是我设法提供的代码,但是没有按预期工作。
After this process, sizeof(array) always returns 8, which I assume means that the array is only being resized once. 在此过程之后,sizeof(array)始终返回8,我认为这意味着该数组仅被调整一次大小。 (sizeof(int) is 4 bits)
(sizeof(int)是4位)
Trying to output the array results in multiple instances of the first input variable. 尝试输出数组会导致第一个输入变量的多个实例。
OUTPUT CODE 输出代码
for(int s=0;s<sizeof(array)/sizeof(int);s++){
printf("%i\n",array[i]);
}
ORIGINAL CODE: 原始代码:
int i;
int size = 1;
int *array = malloc(size * sizeof(int));
int position = 0;
do{
i = getchar() - 48;
f (i != -16 && i != -38 && i != 0) {
array[position] = i;
position++;
size++;
*array = realloc(array, size * sizeof(int));
}
} while (i != 0);
UPDATED STILL NOT WORKING CODE 更新后仍无法正常工作的代码
int i;
int size = 1;
int *array = malloc(size * sizeof(int));
int position = 0;
do{
i = getchar() - 48;
f (i != -16 && i != -38 && i != 0) {
array[position] = i;
position++;
size++;
array = realloc(array, size * sizeof(int));
}
} while (i != 0);
array = realloc(...)
not *array
. 不是
*array
。 Per the realloc docs , realloc
returns the pointer, which you can store directly in your array
pointer. 根据realloc docs ,
realloc
返回指针,您可以将其直接存储在array
指针中。
Edit One thing that will make your life easier: use char
constants instead of raw numbers. 编辑让您的生活更轻松的一件事:使用
char
常量而不是原始数字。 Eg, 例如,
i = getchar();
if(i != ' ' && i != '\n' && i != '0') {
/* 48-16 48-38 48-0 right? */
array[position] = i - '0'; /* '0' = 48 */
One thing that jumps out at me: inside your loop, this line: 跳到我身上的一件事:在循环中,此行:
*array = realloc(array, size * sizeof(int));
should instead be: 相反,应为:
array = realloc(array, size * sizeof(int));
In the original version, you were sticking the result of realloc
in the first element of the array by dereferencing the pointer first. 在原始版本中,您通过先取消引用指针将
realloc
的结果粘贴在数组的第一个元素中。 Without the asterisk, you're reassigning the array itself. 没有星号,您将重新分配数组本身。
(With some copy-paste from my comment:) sizeof(array)
returns 8 because it equals sizeof(int*)
( array
is type int*
) which is 8 (you're probably compiling as 64-bit). (从我的评论中复制粘贴:)
sizeof(array)
返回8,因为它等于sizeof(int*)
( array
类型为int*
),后者为8(您可能将其编译为64位)。 sizeof
doesn't work how you think for pointers to arrays. sizeof
对您如何看待指向数组的指针不起作用。
Similarly, your output code is wrong, for the same reason. 同样,出于相同的原因,您的输出代码是错误的。 You only print the first two elements because
sizeof(array)/sizeof(int)
will always be 8/4=2
. 您只打印前两个元素,因为
sizeof(array)/sizeof(int)
始终为sizeof(array)/sizeof(int)
8/4=2
。 It should be 它应该是
for(int s=0;s<size;s++){
printf("%i\n",array[s]);
}
(note also changed index variable i
to s
) where size
is the variable from your other code chunk(s). (还要注意将索引变量
i
更改为s
),其中size
是您其他代码块中的变量。 You cannot find the length of the array from sizeof
if it's dynamically allocated with pointers; 如果数组是使用指针动态分配的,则无法从
sizeof
找到数组的长度。 that's impossible. 这不可能。 Your code must "remember" the size of your array.
您的代码必须“记住”数组的大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.