有glibc附带的C的自动调整大小的数组/动态数组实现吗? [英] Is there an auto-resizing array/dynamic array implementation for C that comes with glibc?
本文介绍了有glibc附带的C的自动调整大小的数组/动态数组实现吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在glibc或C的任何标准Linux库中是否有动态数组实现?我想要添加到列表中,而不用担心它的大小。我知道std :: vector存在于C ++,但是我需要C等价物。
Is there a dynamic array implementation in glibc or any of the standard Linux libraries for C? I want to be able to add to a list without worrying about its size. I know std::vector exists for C++, but I need the C equivalent.
推荐答案
我想你在想href =http://opengroup.org/onlinepubs/007908775/xsh/realloc.html =noreferrer> realloc 。但是最好在结构中包含列表以跟踪其当前的长度。
I guess you are thinking of realloc. But its better to wrap a list in a structure to keep track of its current length
示例API
struct s_dynamic_array {
int allocated; /* keep track of allocated size */
int usedLength; /* keep track of usage */
int *array; /* dynamicaly grown with realloc */
};
typedef struct s_dynamic_array s_dynamic_array;
s_dynamic_array *new_dynamic_array(int initalSize);
void free_dynamic_array(s_dynamic_array *array);
int size_of_dynamic_array(s_dynamic_array *array);
s_dynamic_array *add_int_to_dynamic_array(s_dynamic_array *array, int value);
int int_at_index(s_dynamic_array *array, int index);
这篇关于有glibc附带的C的自动调整大小的数组/动态数组实现吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文