gpt4 book ai didi

c - 从结构的动态数组中删除元素

转载 作者:太空宇宙 更新时间:2023-11-04 02:27:53 25 4
gpt4 key购买 nike

我在用 C 语言工作

我有一个名为 Entity 的结构,我创建了一个该结构的动态数组。然后我尝试从数组中删除一个元素,但我没有得到我想要的行为。

这是我使用的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Entity
{
int x, y;
int velX, velY;
}Entity;

int remove_element(Entity** array, int sizeOfArray, int indexToRemove)
{
int i;

printf("Beginning processing. Array is currently: ");
for (i = 0; i < sizeOfArray; ++i)
printf("%d ", (*array)[i].x);
printf("\n");

Entity* temp = malloc((sizeOfArray - 1) * sizeof(Entity)); // allocate an array with a size 1 less than the current one

memmove(
temp,
*array,
(indexToRemove+1)*sizeof(Entity)); // copy everything BEFORE the index

memmove(
temp+indexToRemove,
(*array)+(indexToRemove+1),
(sizeOfArray - indexToRemove)*sizeof(Entity)); // copy everything AFTER the index


printf("Processing done. Array is currently: ");
for (i = 0; i < sizeOfArray - 1; ++i)
printf("%d ", (temp)[i].x);
printf("\n");

free (*array);
*array = temp;
return 0;

}

int main()
{
int i;
int howMany = 20;

Entity* test = malloc(howMany * sizeof(Entity*));

for (i = 0; i < howMany; ++i)
(test[i].x) = i;

remove_element(&test, howMany, 14);
--howMany;

return 0;
}

我得到的输出:

Beginning processing. Array is currently: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Processing done. Array is currently: 0 1 2 3 4 1866386284 6 7 8 9 10 11 12 13 15 16 17 18 19

然后程序在 free (*array); 行崩溃。我希望我的第二行是 0 1 2 3 4 5 6 7 8 9 10 11 12 13 15 16 17 18 19。

我该如何解决我的问题?

最佳答案

首先,您已经分配了内存空间来容纳 20 个 Enity*。然后您取消了对它的引用(并且它包含的值是不确定的)。这是未定义的行为。所有的故事到此结束。

但让我们分析一下您最想要的是什么。

Entity* test = malloc(howMany * sizeof(Entity));
^^^^^^^

是你想要的。因为只有这样做你才会得到成员元素 x 等等。

此外,如果您正在考虑 0 索引,那么 memmove 调用应该是

memmove(temp, *array, (indexToRemove)*sizeof(Entity)); 
memmove(temp+indexToRemove, (*array)+(indexToRemove+1),
(sizeOfArray - indexToRemove - 1)*sizeof(Entity));

这两个更改足以解决您面临的问题并实现正确的行为。 (如果这就是您的代码中的全部内容)。

此外,根据标准,main() 应该这样声明,以防它不带任何参数 int main(void)。完成使用后释放动态分配的内存。您还应该检查 malloc 的返回值 - 如果它失败,它会返回 NULL 并且您应该处理这种情况。

关于c - 从结构的动态数组中删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48356905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com