op_prg_mem_free()

摘要

释放先前已分配的内存块。

注意: 该内核过程是一个仅为向后兼容而保留的宏调用。新代码中请使用 prg_mem_free()。

语法

op_prg_mem_free (ptr)
参数 类型 说明
ptr void* 指向待释放内存块的指针
传入该内核过程的指针通常应限于最初由内核过程 op_prg_mem_alloc() 获得的指针(不过如下方示例一节所示,op_prg_gdf_read() 产生的动态分配数据结构也可以用该内核过程释放)。

返回类型

void

无返回值。

示例

/* 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);

详情

动态分配的内存如果在仿真中不再有用,应当释放以便复用。仿真过程中不断创建的短生命周期数据结构若不释放,会使内存占用持续增长,可能导致仿真因内存不足而不必要地中止。

用途

提供回收在仿真中已完成使命的动态分配内存的机制。仿真中通用动态分配内存的生命周期如下:

  1. 通过 op_prg_mem_alloc() 分配一块内存,并将其转换为合适的数据结构。
  2. 对该数据结构进行赋值,用于数据存储或通信。
  3. 结构的作用结束后,其内存块由 op_prg_mem_free() 释放。

错误

  • 程序中止:段错误(由于 ptr 参数无效)。
  • 诊断错误:无法释放未知内存类型(由于将该内核过程应用于通过仿真内核之外的接口分配的内存)。

参考

本站文档由 北京智网畅联科技有限公司 整理、翻译并提供 · Documentation compiled and provided by Beijing ZNetLink Technology Co., Ltd.

交流群 · QQ Group:OPNET/智网妙算网络仿真交流群(群号 486383494)

原文档 © 2017 Riverbed Technology · 本站为公益翻译,由 AI 辅助完成,请仔细甄别

Original documentation © 2017 Riverbed Technology. Community translation, AI-assisted. Contact: jiazequn@znetlink.com