ç动态成长阵列 [英] C dynamically growing array

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

问题描述

我有一个程序,读取游戏中的实体的原始列表中,我打算做一个数组控股实体的不确定数量的索引号(INT),用于处理各种事情。我想避免使用过多的内存或CPU保持这样的索引...

一个快速和肮脏的溶液I迄今使用是声明,在主处理功能(本地焦点)的尺寸的最大游戏实体的阵列,而另一个整数跟踪多少已被添加到该名单。
这是不能令人满意的,因为每个列表可以保存3000+阵列,这不算多,但感觉像是一种浪费,因为我将可能使用该解决方案为6-7列出了不同的功能。

我还没有发现任何C(不是C ++或C#),具体的解决方案,以实现这一目标。我可以使用指针,但我有点害怕使用它们(除非它是唯一可能的方式)。

该数组不离开本地函数范围(它们被传递给函数,然后被丢弃),在改变事情的情况下。

如果指针是唯一的解决办法,我怎么能跟踪它们,以避免泄露?


解决方案

  

我可以使用指针,但我有点害怕使用它们。


如果你需要一个动态数组,你无法逃避的指针。你为什么害怕有关系吗?他们不会咬人(只要你小心,这是)。有没有在C中没有内置动态数组,你只好自己写一个。在C ++中,你可以使用内置的 的std ::矢量 类。 C#和几乎所有其他的高级语言也有管理动态数组为你一些类似的类。

如果你打算写自己的,这里的东西让你开始:最有活力的数组实现工作与一些(小)默认大小的数组出发,然后添加一个新的元素时,只要您运行的空间,双数组的大小。正如你可以在下面的例子中看到,它不是很难在所有:(我不再赘述安全检查)

  typedef结构{
  INT *阵列;
  为size_t使用;
  为size_t的大小;
}数组;无效initArray(数组*一,为size_t INITIALSIZE){
  A->数组=(INT *)malloc的(INITIALSIZE *的sizeof(INT));
  A->作为= 0;
  A->大小= INITIALSIZE;
}无效insertArray(数组*一,诠释元素){
  如果(A->作为== A->大小){
    A->大小* = 2;
    A->数组=(INT *)的realloc(A->阵,A->大小*的sizeof(INT));
  }
  A->阵[A->作为++] =元素;
}无效freeArray(数组*一){
  免费(A->数组);
  A->阵= NULL;
  A->作为= A->大小= 0;
}

使用它只是简单:

 阵列一个;
INT I;initArray(&放大器;一,5); //最初5个元素
对于(i = 0; I< 100;我++)
  insertArray(安培; A,I); //自动调整大小根据需要
的printf(%d个\\ N,a.array [9]); //打印10元
的printf(%d个\\ N,a.used); //元素的打印张数
freeArray(&放大器;一个);

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...

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.

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.

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 = (int *)malloc(initialSize * sizeof(int));
  a->used = 0;
  a->size = initialSize;
}

void insertArray(Array *a, int element) {
  if (a->used == a->size) {
    a->size *= 2;
    a->array = (int *)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;
}

Using it is just as simple:

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\n", a.array[9]);  // print 10th element
printf("%d\n", a.used);  // print number of elements
freeArray(&a);

这篇关于ç动态成长阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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