C语言 在 C 中使用 malloc 为 typedef 类型分配空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4252180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:09:43  来源:igfitidea点击:

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

cmalloctypedef

提问by user516888

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 count_table* cTable = malloc(sizeof(count_table*)) , but that doesnt do anything with the size parameter. Am i supposed to allocate space for the list_node_t also? Below is what I am working with.

我不确定我需要使用什么作为 malloc 的参数来在 table_allocate(int) 函数中分配空间。我在想 count_table* cTable = malloc(sizeof(count_table*)) ,但这对 size 参数没有任何作用。我也应该为 list_node_t 分配空间吗?以下是我正在使用的内容。

in the .h file I'm given this signature:

在 .h 文件中,我得到了这个签名:

//create a count table struct and allocate space for it                         
//return it as a pointer                                                        
count_table_t* table_allocate(int);

Here are the structs that I'm supposed to use:

以下是我应该使用的结构:

typedef struct list_node list_node_t;

struct list_node {
  char *key;
  int value;

  //the next node in the list                                                   
  list_node_t *next;
};

typedef struct count_table count_table_t;

struct count_table {
  int size;
  //an array of list_node pointers                                              
  list_node_t **list_array;
};

Thanks!

谢谢!

回答by Jay

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

is wrong. It should be

是错的。它应该是

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

Also, you must allocate memory for list_node_t also seperately.

此外,您还必须单独为 list_node_t 分配内存。

EDIT:

编辑:

Apart from what Clifford has pointed about allocating memory for the list node, I think the memory allocation should also be taken care for the char *keyinside of the list node.

除了 Clifford 所指出的为列表节点分配内存之外,我认为内存分配也应该注意char *key列表节点的内部。

回答by caf

Given that the intis a "size" parameter for the created count_table_t, it appears that you are supposed to both allocate the count_table_titself, as well as initialise its members.

鉴于int是 created 的“大小”参数count_table_t,看来您应该既分配count_table_t自身,又初始化其成员。

Initialising the list_arraymember also involves a memory allocation, so it would look like:

初始化list_array成员还涉及内存分配,因此它看起来像:

count_table_t *table_allocate(int size)
{
    count_table_t *table = malloc(sizeof *table);
    int i;

    table->size = size;
    table->list_array = malloc(size * sizeof table->list_array[0]);
    for (i = 0; i < size; i++)
        table->list_array[i] = NULL;

    return table;
}

However, you also need to check for some error conditions: the multiplication of sizeby sizeof table->list_array[0]could overflow, and either of the malloc()calls could fail. So the function should actually look like this:

但是,您还需要检查一些错误情况:sizeby的乘法sizeof table->list_array[0]可能会溢出,并且任何一个malloc()调用都可能失败。所以这个函数实际上应该是这样的:

count_table_t *table_allocate(int size)
{
    count_table_t *table;
    int i;

    /* Check for overflow in list allocation size */
    if (size < 0 || size > (size_t)-1 / sizeof table->list_array[0])
        return NULL;

    table = malloc(sizeof *table);

    if (table == NULL)
        return NULL;

    table->size = size;
    table->list_array = malloc(size * sizeof table->list_array[0]);

    if (table->list_array == NULL) {
        free(table);
        return NULL;
    }

    for (i = 0; i < size; i++)
        table->list_array[i] = NULL;

    return table;
}

(Note that (size_t)-1is a constant equal to the maximumvalue of a size_t, which is the type of the parameter to malloc()).

(注意,(size_t)-1为常数等于该最大的一个值size_t,这是参数的类型malloc())。

回答by Clifford

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

您的建议:count_table* cTable = malloc(sizeof(count_table*))只会为指向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::nextupdated accordingly. Maintaining a pointer to the last node added would make adding nodes faster.

每个列表节点将被单独分配,cTable->size 和 cTable->list_array 和最后list_node_t::next更新相应。维护指向添加的最后一个节点的指针将使添加节点更快。

I am not sure why count_table::list_arrayis of type list_node_t**rather than just list_node_t*(and equally called list_arrayrather 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

我不确定为什么count_table::list_array是 typelist_node_t**而不是 just list_node_t*(并且同样被称为list_array而不是 just list)。您的意图是它同时是一个数组和一个列表吗?那会有些多余。该成员只需要一个指向第一个节点的指针,然后通过访问后续节点list_node::next

回答by jer

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 cTableever changes, you won't have to adjust two parts to that line, just the type.

如果类型cTable发生变化,这将有助于您,您不必将两个部分调整到该行,只需调整类型。