【发布时间】:2015-05-11 05:38:23
【问题描述】:
我在 c 中有一个算法,其中多次使用 malloc 分配内存。我想编写一个函数,在程序全部完成后释放该内存,但我不确定如何构造它。会不会只是多次调用free()?我对 C 和内存分配相当陌生,因此我们将不胜感激。
程序:
typedef struct State State;
typedef struct Suffix Suffix;
struct State { /* prefix + suffix list */
char* pref[NPREF]; /* prefix words */
Suffix* suf; /* list of suffixes */
State* next; /* next in hash table */
};
struct Suffix { /* list of suffixes */
char * word; /* suffix */
Suffix* next; /* next in list of suffixes */
};
【问题讨论】:
-
我建议只包含相关的sn-ps代码
-
标准警告:请do not cast
malloc()和C中的家人的返回值。 -
“清除内存”是什么意思?用零代替?什么原因?