通用程序算法和数据结构
In C, what’s the standard or common data structure for a list of objects?
在C中 ,对象列表的标准或通用数据结构是什么?
In C, one common way to store a list/collection of objects of the same type is to use one-dimensional arrays.
在C语言中 ,存储相同类型对象的列表/集合的一种常用方法是使用一维数组。
In C, an array is a collection of data objects of the same type. An array is in a contiguous memory area. The lowest address of the memory area corresponds to the first element in the array and all other elements follow it one by one.
在C语言中,数组是相同类型的数据对象的集合。 阵列位于连续的内存区域中。 存储区的最低地址对应于数组中的第一个元素,所有其他元素一个接一个地跟随它。
Arrays can be statically allocated or dynamically allocated:
数组可以静态分配也可以动态分配:
// statically allocated
// declare a static array a1 of 100 ints
int a1[100];
// dynamically allocated on the heap
// allocate a dynamical array a1 of 100 ints
int a2_len = 100;
int *a2 = (int *)malloc(sizeof(int) * a2_len);
Element access time in an array has O(1) complexity. Memory management like increasing the size of an array should be done grammatically. Some library functions may help though. For example, void *realloc(void *ptr, size_t size) changes the size of the memory block pointed to by ptr to size bytes. The contents will be unchanged in the range from the start of the region up to the minimum of the old and new sizes, and returns a pointer to the newly allocated memory which may be different from ptr.
数组中元素的访问时间具有O(1)复杂度。 像增加数组大小一样的内存管理应该在语法上进行。 一些库函数可能会有所帮助。 例如, void *realloc(void *ptr, size_t size)将ptr指向的内存块的大小更改为size字节。 内容将会是从范围不变启动该地区提升至最低新旧和大小,并返回一个指向新分配的内存可以来自不同的ptr 。
One example to sum the a1 and a2 together into a2:
一个将a1和a2总计为a2的示例:
int i = 0;
for (i = 0; i < 100; ++i) {
a2[i] += a1[i];
}
通用程序算法和数据结构

4176

被折叠的 条评论
为什么被折叠?



