Using malloc in C to allocate space for a typedef'd type

前端 未结 4 897
逝去的感伤
逝去的感伤 2021-01-14 12:39

I\'m not sure exactly what I need to use as an argument to malloc to allocate space in the table_allocate(int) function. I was thinking just

4条回答
  •  伪装坚强ぢ
    2021-01-14 13:06

    Your suggestion: count_table* cTable = malloc(sizeof(count_table*)) would only allocate space for a pointer to a count_table.

    You'd need

    count_table* cTable = malloc(sizeof(count_table) ) ;
    

    Each list node would be separately allocated and cTable->size and cTable->list_array and the last list_node_t::next updated accordingly. Maintaining a pointer to the last node added would make adding nodes faster.

    I am not sure why count_table::list_array is of type list_node_t** rather than just list_node_t* (and equally called list_array rather than just list). Is it your intention that it is both an array and a list at the same time? That would be somewhat redundant. The member need only be a pointer to the first node, successive nodes are then accessed via list_node::next

提交回复
热议问题