C 动态增长数组 [英] C dynamically growing array

查看:32
本文介绍了C 动态增长数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序可以读取游戏内实体的原始"列表,我打算制作一个包含不确定数量实体的索引号 (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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆