小编use*_*495的帖子

在内联结构内循环分配链表不分配内存

我在循环中分配链表有奇怪的问题.

考虑一个简化的源代码:

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)

c malloc structure inline linked-list

2
推荐指数
1
解决办法
269
查看次数

标签 统计

c ×1

inline ×1

linked-list ×1

malloc ×1

structure ×1