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

前端 未结 4 920
逝去的感伤
逝去的感伤 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:02

    In addition to the other posters who point out that you're only allocating enough space for the pointer, not the space the data you want will occupy, I strongly urge you to do things like this:

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

    This will help you in case the type of cTable ever changes, you won't have to adjust two parts to that line, just the type.

提交回复
热议问题