op_prg_list_size()
摘要
获取指定列表的长度(即列表包含的元素数量)。
注意: 该内核过程是一个仅为向后兼容而保留的宏调用。新代码应使用 prg_list_size()。
语法
op_prg_list_size (list_ptr)
| 参数 | 类型 | 说明 |
|---|---|---|
list_ptr |
List* |
指向目标列表的指针 列表指针可以从多个内核过程获得:空列表可由 op_prg_list_create() 获得;表示文件内容的列表可由 op_prg_gdf_read() 获得;列表可由用户代码分配并通过 op_prg_list_init() 初始化。 |
返回类型
int
指定列表的大小(即列表中存储的元素地址数量)。
[空列表的大小为零。]
示例
/* open and read in the contents of a GDF file */
list_ptr = op_prg_gdf_read (gdf_file_name);
/* loop through the contents of the GDF contents list, */
/* printing each line */
list_size = op_prg_list_size (list_ptr);
for (i = 0; i < list_size; i++)
printf ("line %d: [%s]\n", i, (char*) op_prg_list_access (list_ptr, i));
/* deallocate the elements on the list */
op_prg_list_free (list_ptr);
/* deallocate the list itself */
op_prg_mem_free (list_ptr);
详情
当列表通过内核过程 op_prg_list_create() 创建,或通过内核过程 op_prg_list_init() 初始化时,它是空的,大小为零。通过内核过程 op_prg_list_insert() 向列表插入元素时,每插入一次列表大小加一。通过内核过程 op_prg_list_remove() 从列表移除元素时,每移除一次列表大小减一。
注意: 该内核过程不是 MT 安全的,如 “Multi-Threading Safety“ 第 11 页所述。对共享列表的访问必须由外部串行化。
用途
提供一种确定列表所包含元素数量的机制。当需要对列表进行顺序扫描(通过内核过程 op_prg_list_access())或消耗并处理列表(通过内核过程 op_prg_list_remove())时,这通常很有用。列表扫描或处理可以通过构造一个循环来实现,该循环对列表中的每个元素各执行一次,循环次数由本内核过程给出。在列表扫描的情况下(循环过程中列表大小不变),循环索引值可以作为位置索引使用。
错误
- 程序中止:段错误。(由于列表指针无效或列表格式错误)
参考
- 使用内核过程 op_prg_list_insert() 向列表中插入新元素
- 使用内核过程 op_prg_list_remove() 从列表中移除元素
- 使用内核过程 op_prg_list_access() 获取列表元素的地址而不将其移除