c - glibc 是否有针对 C 的自动调整大小数组/动态数组实现?

标签 c data-structures glibc

有没有dynamic array在 glibc 或 C 的任何标准 Linux 库中实现?我希望能够添加到列表而不用担心它的大小。我知道 C++ 存在 std::vector,但我需要 C 等价物。

最佳答案

我猜你在想realloc .但最好将 list 包裹在结构中以跟踪其当前长度

示例 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);

关于c - glibc 是否有针对 C 的自动调整大小数组/动态数组实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/575914/

相关文章:

c - 为什么我的代码有效? C中的数组排序

data-structures - HashMap 和 HashTable 纯粹在数据结构上的区别

linux - 从文件读取和写入

c - timeval和微秒之间的精确转换

objective-c - 为什么这个 uint32_t 在内存中是这样排序的?

c - 如果点在三角形内部(当点在三角形的边界上时有帮助)

algorithm - 将 A 转换为 B 所需的最少步骤数

data-structures - 将 Haskell Edison API 和 Core 移植到 F# 有什么好处吗?

c - 不知道为什么我收到 Glibc 检测到错误

c - 为什么严格的别名规则不适用于 int* 和 unsigned*?