C动态增长数组 [英] C dynamically growing array
问题描述
我有一个程序可以读取游戏中实体的原始"列表,我打算创建一个数组来保存不确定数量的实体的索引号 (int),以处理各种事情.我想避免使用过多的内存或 CPU 来保存此类索引...
I have a program that reads a "raw" list of in-game entities, and I intend to make an array holding an index number (int) of an indeterminate number of entities, for processing various things. I would like to avoid using too much memory or CPU for keeping such indexes...
到目前为止,我使用的一个快速而肮脏的解决方案是在主处理函数(本地焦点)中声明具有最大游戏实体大小的数组,以及另一个整数来跟踪已添加到列表.这并不令人满意,因为每个列表都包含 3000 多个数组,虽然不算多,但感觉很浪费,因为我可能会使用 6-7 个列表的解决方案来实现不同的功能.
A quick and dirty solution I use so far is to declare, in the main processing function (local focus) the array with a size of the maximum game entities, and another integer to keep track of how many have been added to the list. This isn't satisfactory, as every list holds 3000+ arrays, which isn't that much, but feels like a waste, since I'll possible use the solution for 6-7 lists for varying functions.
我还没有找到任何 C(不是 C++ 或 C#)特定的解决方案来实现这一点.我可以使用指针,但我有点害怕使用它们(除非这是唯一可能的方法).
I haven't found any C (not C++ or C#) specific solutions to achieve this. I can use pointers, but I am a bit afraid of using them (unless it's the only possible way).
数组不会离开本地函数范围(它们将被传递给函数,然后被丢弃),以防万一发生变化.
The arrays do not leave the local function scope (they are to be passed to a function, then discarded), in case that changes things.
如果指针是唯一的解决方案,我如何跟踪它们以避免泄漏?
If pointers are the only solution, how can I keep track of them to avoid leaks?
推荐答案
我可以使用指针,但我有点害怕使用它们.
I can use pointers, but I am a bit afraid of using them.
如果你需要一个动态数组,你不能转义指针.你为什么害怕呢?他们不会咬人(只要你小心,就是这样).C 中没有内置的动态数组,您只需要自己编写一个即可.在 C++ 中,您可以使用内置的 std::vector
类.C# 和几乎所有其他高级语言也有一些类似的类可以为您管理动态数组.
If you need a dynamic array, you can't escape pointers. Why are you afraid though? They won't bite (as long as you're careful, that is). There's no built-in dynamic array in C, you'll just have to write one yourself. In C++, you can use the built-in std::vector
class. C# and just about every other high-level language also have some similar class that manages dynamic arrays for you.
如果您确实打算编写自己的,这里有一些东西可以帮助您开始:大多数动态数组实现都是从一些(小)默认大小的数组开始工作的,然后每当您在添加新元素时空间不足时, 数组大小的两倍.正如您在下面的示例中所看到的,它一点也不难:(为简洁起见,我省略了安全检查)
If you do plan to write your own, here's something to get you started: most dynamic array implementations work by starting off with an array of some (small) default size, then whenever you run out of space when adding a new element, double the size of the array. As you can see in the example below, it's not very difficult at all: (I've omitted safety checks for brevity)
typedef struct {
int *array;
size_t used;
size_t size;
} Array;
void initArray(Array *a, size_t initialSize) {
a->array = malloc(initialSize * sizeof(int));
a->used = 0;
a->size = initialSize;
}
void insertArray(Array *a, int element) {
// a->used is the number of used entries, because a->array[a->used++] updates a->used only *after* the array has been accessed.
// Therefore a->used can go up to a->size
if (a->used == a->size) {
a->size *= 2;
a->array = realloc(a->array, a->size * sizeof(int));
}
a->array[a->used++] = element;
}
void freeArray(Array *a) {
free(a->array);
a->array = NULL;
a->used = a->size = 0;
}
使用就这么简单:
Array a;
int i;
initArray(&a, 5); // initially 5 elements
for (i = 0; i < 100; i++)
insertArray(&a, i); // automatically resizes as necessary
printf("%d
", a.array[9]); // print 10th element
printf("%d
", a.used); // print number of elements
freeArray(&a);
这篇关于C动态增长数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!