我想创建和使用类似以下的东西:
struct mystruct {
mystruct * AnOtherMyStruct; //mystruct must have a pointer to an other mystruct inside
...some stuffs...
};
我应该使用 "typedef struct" 吗?有哪些优缺点?
在分配内存时,有人告诉我用 calloc 比 malloc 好,因为它做同样的工作但更好...
我的结果会是这样的:
struct (or typedef struct) mystruct {
mystruct * AnOtherMyStruct;
...
};
int main () {
malloc or calloc =...
free(...);
你认为,在考虑到这些操作会经常执行的情况下,哪种方式是分配和释放结构体的最佳选择?
calloc
)在语法上更适合序列分配,并具有零填充分配块的附加功能。无论这是否是您使用的有益功能,只有您自己可以决定。 - WhozCraig