我在循环中分配链表有奇怪的问题.
考虑一个简化的源代码:
struct main_s {
minor_s minor_structure; (inline)
};
struct minor_s {
list_s *first_dir;
};
struct list_s {
anotherlist_s *first_object;
list_s *next;
};
struct anotherlist_s {
//multiple fields
};
Run Code Online (Sandbox Code Playgroud)
我有一个基本的init/deinit函数,如:
struct main_s *main_s_init();
void main_s_deinit();
Run Code Online (Sandbox Code Playgroud)
而现在我有点像循环分配:
im passing to this function main_s->minor_structure.first_dir and, how_many parameter, defining how many linked nodes going to be initiated.
void loop_inittiation(struct list_s *list, int how_many) {
int i;
struct list_s *tmp = list;
for(i = 0; i < how_many; i++) {
tmp = malloc(sizeof(struct list_s)); …Run Code Online (Sandbox Code Playgroud)