将灵活数组成员分配为结构 [英] allocating flexible array members as a struct

查看:105
本文介绍了将灵活数组成员分配为结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对指针和东西了解得更多,但是我不知道我在这里做错了什么. 如果我有 字符*(*数据)[] 那将被解释为指向char指针数组的指针",对吗? 然后我有一个像这样的结构,类型定义为myStruct,可能是多余的,但这是重点:

I understand more about pointers and stuff but I have no idea what I am doing wrong here. if i Have char *(*data)[] That would just be interpreted as "a pointer to an array of char pointers", right? Then I have a struct like this, typedef'd to be myStruct, redundant as it may be, but that's aside the point:

    typedef struct myStruct myStruct;
    struct myStruct{
      int size;
      char *name;
      myStruct *(*array)[];
}

环顾四周,发现类似的帖子,

Having looked around the site for similar posts, I got something like this:

    //let's say allocating 5 spaces for this case
    myStruct *a = malloc(sizeof(myStruct)+ sizeof(struct myStruct *)*5);

我确定分配给该结构的数字是数组的大小. 我对此不太了解,如果是结构,它如何工作? 这里的计划是拥有这个结构,它包含5个myStruct的数组.我也必须分别分配它们吗?这样吗?

I am sure that the number I allocated the struct with is the size of the array. I can't quite get my head wrapped around this, how does it work if it's a struct? The plan here is to have this struct, and it contains an array of 5 myStruct's. Do I have to allocate them separately as well? like this?

    a->array[0] = malloc( .... ) 

我尝试过,它一直给我一个错误,指出无效使用未指定边界的数组. 我做错了什么或该如何解决? 谢谢

I tried and it keeps giving me an error saying Invalid use of array with unspecified bounds. What am I doing wrong or how Can i fix this? Thank you

推荐答案

从您的评论看来,您似乎想要一个指向结构指针数组的指针,而不是指向结构数组指针,因为"char *数组也具有两个间接级别.

From your comment, it sounds like you want a pointer to an array of pointers to structures, rather than a pointer to an array of structures, since "pointer to an array of char *" also has two levels of indirection.

区别在于:

  1. 指向结构的指针:

  1. A pointer to a structure:

指向结构数组的指针:

A pointer to an array of structures:

指向结构指针数组的指针:

A pointer to an array of pointers to structures:

假设您想要#3,则可以这样做(在传统" C语言中):

Assuming you want #3, you can do it like so (in "traditional" C):

typedef struct myStruct myStruct;

struct myStruct
{
    int        size;
    char      *name;
    myStruct **array;
};

myStruct *allocate_node(char *name, int size)
{
    myStruct *p_node;
    if (size < 0)
        size = 0;
    p_node = calloc(1, sizeof(myStruct));
    p_node->name = name;
    p_node->size = size;
    p_node->array = calloc(1, size * sizeof(myStruct *));

    return p_node;
}

void expand_node_child_array(myStruct *p_node, int size_to_add)
{
    if (size_to_add < 1 || p_node == NULL)
        return;
    if (p_node->array == NULL)
    {
        p_node->size = size_to_add;
        p_node->array = calloc(1, size_to_add * sizeof(myStruct *));
    }
    else
    {
        p_node->array = realloc(p_node->array, (p_node->size + size_to_add) * sizeof(myStruct *));
        memset(p_node->array + p_node->size * sizeof(myStruct *), 0, size_to_add * sizeof(myStruct *));
        p_node->size += size_to_add;
    }
}

myStruct *get_child_node(myStruct *p_node, int index)
{
    if (index < 0 || index >= p_node->size)
        return 0;
    return p_node->array[index];
}

int set_child_node(myStruct *p_node, int index, myStruct *p_child)
{
    if (index < 0 || index >= p_node->size)
        return FALSE;
    p_node->array[index] = p_child;
    return TRUE;
}

void free_node(myStruct **pp_node)
{
    // Free p_node and the array but DO NOT free the children
    if (pp_node == NULL || *pp_node == NULL)
        return;
    if ((*pp_node)->array != NULL)
        free((*pp_node)->array);
    free((*pp_node));
    *pp_node = NULL;
}

void free_node_and_children(myStruct **pp_node)
{
    int iChild;

    if (pp_node == NULL || *pp_node == NULL)
        return;
    for (iChild = 0; iChild < (*pp_node)->size; iChild++)
    {
        myStruct *p_child = get_child_node((*pp_node), iChild);
        if (p_child != NULL)
            free_node_and_children(&p_child);
        set_child_node((*pp_node), iChild, NULL);
    }
    free_node(pp_node);
}

更新

根据C99标准语法,灵活数组是一个可变长度数组,它出现在结构的尾部,其实际长度是在运行时设置的.在内存中看起来像这样:

A flexible array, under the C99 standard syntax, is a variable-length array that appears at the tail of a structure and whose actual length is set at run time. It looks like this in memory:

假设您的编译器支持这种语法(并非所有语法都支持),则可以这样声明:

Assuming your compiler supports this syntax (not all do), you declare it like this:

struct myStruct
{
    Type  array_of_type[]; /* AT THE END OF THE STRUCT ONLY */
};

"myStruct"的代码变为:

And the code for "myStruct" becomes:

typedef struct myStruct myStruct;

struct myStruct
{
    int        size;
    char      *name;
    myStruct  *array[];
};

myStruct *allocate_node(char *name, int size)
{
    myStruct *p_node;
    if (size < 0)
        size = 0;
    p_node = calloc(1, sizeof(myStruct) + size * sizeof(myStruct *));
    p_node->name = name;
    p_node->size = size;

    return p_node;
}

myStruct *get_child_node(myStruct *p_node, int index)
{
    if (index < 0 || index >= p_node->size)
        return NULL;
    return p_node->array[index];
}

int set_child_node(myStruct *p_node, int index, myStruct *p_child)
{
    if (index < 0 || index >= p_node->size)
        return FALSE;
    p_node->array[index] = p_child;
    return TRUE;
}

void free_node(myStruct **pp_node)
{
    if (pp_node == NULL || *pp_node == NULL)
        return;
    free((*pp_node));
    *pp_node = NULL;
}

void free_node_and_children(myStruct **pp_node)
{
    int iChild;

    if (pp_node == NULL || *pp_node == NULL)
        return;
    for (iChild = 0; iChild < (*pp_node)->size; iChild++)
    {
        myStruct *p_child = get_child_node((*pp_node), iChild);
        if (p_child != NULL)
            free_node_and_children(&p_child);
        set_child_node((*pp_node), iChild, NULL);
    }
    free_node(pp_node);
}

如果编译器未编译,请参见此处以获取一些解决方法.

If you compiler does not, see here for some workarounds.

使用灵活的数组,扩展数组将需要重新分配节点本身并固定对它的所有引用,这在指针数组指针"设计中是不需要的.

With flexible arrays, expanding the array would require re-allocating the node itself and fixing all references to it, something not required in the "pointer to array of pointers" design.

您使用的语法:

    myStruct  *(*array)[];

读为指向数组的指针指向结构的指针的大小未知",而不是

should be read as "a pointer to array(s) of unknown size of pointers to structures", rather than

    myStruct  **array;

是指向结构的指针",或(例如):

which is "a pointer to pointer(s) to structures", or (e.g.):

    myStruct *(*array)[4];

是指向长度为4的指针的数组的指针.

which is "a pointer to array(s) of length 4 of pointers.

您的语法实际上产生了内存映射3,但是访问单个元素有点麻烦,因为您必须显式地获取指向未知大小的数组"的第零个元素的指针,即(*p_node->array).因此,对#3中的函数进行了如下修改:

Your syntax actually produces memory map #3, however accessing the individual elements is a bit more awkward because you have to explicitly get a pointer to the zeroth element of the "array of unknown size", which is (*p_node->array). Thus the functions from #3 are modified as follows:

void expand_node_child_array(myStruct *p_node, int size_to_add)
{
    if (size_to_add < 1 || p_node == NULL)
        return;
    if (p_node->array == NULL)
    {
        p_node->size = size_to_add;
        p_node->array = calloc(1, size_to_add * sizeof(myStruct *));
    }
    else
    {
        p_node->array = realloc(p_node->array, (p_node->size + size_to_add) * sizeof(myStruct *));
        memset((*p_node->array) + p_node->size * sizeof(myStruct *), 0, size_to_add * sizeof(myStruct *));
        p_node->size += size_to_add;
    }
}

myStruct *get_child_node(myStruct *p_node, int index)
{
    if (index < 0 || index >= p_node->size)
        return NULL;
    return (*p_node->array)[index];
}

int set_child_node(myStruct *p_node, int index, myStruct *p_child)
{
    if (index < 0 || index >= p_node->size)
        return FALSE;
    (*p_node->array)[index] = p_child;
    return TRUE;
}

最后,这两种体系结构的测试代码:

And finally, the test code for either architecture:

void dump_nodes_recursive(myStruct *p_node, int level)
{
    if (p_node == NULL)
    {
        printf("%*s", 4*level, " ");
        printf("NULL\n");
    }
    else
    {
        int iChild;
        printf("%*s", 4*level, " ");
        printf("Node: Name=\"%s\", array size=%d\n", p_node->name, p_node->size);
        for (iChild = 0; iChild < p_node->size; iChild++)
        {
            myStruct *p_child = get_child_node(p_node, iChild);
            printf("%*s", 4*level, " ");
            printf("Child [%d]:\n", iChild);
            dump_nodes_recursive(p_child, level+1);
        }
    }
}

void dump_nodes(myStruct *p_node)
{
    dump_nodes_recursive(p_node, 0);
}

void test_my_struct()
{
    myStruct *p_top = allocate_node("top", 4);
    myStruct *p_child0 = allocate_node("child0", 1);
    myStruct *p_child1 = allocate_node("child1", 5);
    myStruct *p_child2 = allocate_node("child2", 0);
    myStruct *p_child3 = allocate_node("child3", 0);
    myStruct *p_child00 = allocate_node("child00", 0);

    set_child_node(p_top, 0, p_child0);
    set_child_node(p_top, 1, p_child1);
    set_child_node(p_top, 2, p_child2);

    set_child_node(p_top, 3, p_child3);

    set_child_node(p_child0, 0, p_child00);

    dump_nodes(p_top);

    free_node_and_children(&p_top);
}

这篇关于将灵活数组成员分配为结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆