【问题标题】:Struct with union static and dynamic char array具有联合静态和动态字符数组的结构
【发布时间】:2013-02-24 00:24:20
【问题描述】:

我正在创建一个动态分配的链表,其中每个项目都有一个数据元素。这些元素中的大多数都是固定大小的 char 数组,但有些元素的长度可能不同,例如只有 1 或 2 个字符 - 或 1000+。

如果我在静态和动态字符数组的数据元素内使用联合 - 它是否总是会占用:

  • 静态的大小,或者如果 malloc'd 的大小是 malloced 的大小?
  • 静态的大小 + 如果 malloc'd 分配的大小?
  • 指向分配的指针的大小还是静态指针的大小?
  • ...(等等)

没有列表的简化示例。这不是我用数据填充它的方式,仅作为 struct union 的一个示例。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

enum meta_type {LIST, STRING};

struct meta {
    union {
        char clist[128];
        char *cstr;
    } val;
    enum meta_type type;
};

int init_meta(struct meta *meta, enum meta_type type, int size, char *data)
{
    if (type == STRING) {
        if ((meta->val.cstr = malloc(size + 1)) == NULL) {
            puts("Out of memeory");
            return 1;
        }
        sprintf(meta->val.cstr, "%s", data);
    } else {
        memset(meta->val.clist, 0, 128);
        meta->val.clist[0] = data[0];
        meta->val.clist[1] = data[1];
        meta->val.clist[2] = data[2];
    }

    meta->type = type;

    return 0;
}

void free_meta(struct meta *meta)
{
    if (meta->type == STRING)
        free(meta->val.cstr);
}

int main(int argc, char *argv[])
{
    struct meta meta;
    char list[] = {'a', '3', 'f'};

    if (argc > 1) {
        if(init_meta(&meta, STRING, strlen(argv[1]), argv[1]))
            return 1;
    } else {
        init_meta(&meta, LIST, 0, list);
    }

    fprintf(stdout,
        "meta[%d]: %s\n",
        meta.type,
        meta.type == LIST ? meta.val.clist : meta.val.cstr
    );

    free_meta(&meta);

    return 0;
}

@Floris:我测试的一种古怪方式:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>

enum meta_type {LIST, STRING};

#define SIZE_CLIST  128

struct meta {
    enum meta_type type;
    union {
        char clist[SIZE_CLIST];
        char *cstr;
    } val;
};

int init_meta(struct meta **meta, enum meta_type type, int size, char *data)
{
    if (type == STRING) {
        if (offsetof(struct meta, val) == 0) {
            puts("Ups.");
            return 2;
        }
        *meta = malloc(sizeof(struct meta) -
                SIZE_CLIST +
                sizeof(char *)
            );
        (*meta)->val.cstr = malloc(size + 1);
        if ((*meta)->val.cstr == NULL) {
            puts("Out of memeory");
            return 1;
        }
        sprintf((*meta)->val.cstr, "%s", data);
        puts((*meta)->val.cstr);
    } else {
        *meta = calloc(1, sizeof(struct meta));
        (*meta)->val.clist[0] = data[0];
        (*meta)->val.clist[1] = data[1];
        (*meta)->val.clist[2] = data[2];
    }

    (*meta)->type = type;

    return 0;
}

void free_meta(struct meta *meta)
{
    if (meta->type == STRING)
        free(meta->val.cstr);
    free(meta);
}

int main(int argc, char *argv[])
{
    struct meta *meta = NULL;
    char list[] = {'a', '3', 'f'};

    if (argc > 1) {
        if(init_meta(&meta, STRING, strlen(argv[1]), argv[1]))
            return 1;
    } else {
        init_meta(&meta, LIST, 0, list);
    }

    fprintf(stdout,
        "meta[%d] %d: %s\n",
        meta->type,
        sizeof(*meta),
        meta->type == LIST ? meta->val.clist : meta->val.cstr
    );

    free_meta(meta);

    return 0;
}

【问题讨论】:

  • 指向 malloced 数组的指针只占用指针的空间——您不知道指向的内存块的位置。所以我很确定答案是你的第三个选择。你试过这个看看吗?你发现了什么?
  • @Floris:是的,尝试了各种,问题是要准确查看对象的大小。不过,当我计划对每个数据元素进行 malloc 时,我应该接受简单打印sizeof(struct meta) 的提示。我今天很慢。为了好玩而尝试了其他东西,但不会因为它是hackish而使用它。我将编写一个迷你内存管理器,而不是预先分配大小为 N 的地方,我将每个数据元素指向并根据需要分配更多。更新了“骇人听闻的东西”。
  • 我同意 - 确实是“骇人听闻的事情”。我以为我是唯一一个编写这样代码的人......感谢分享 - 很高兴你得到了答案。

标签: c memory struct unions


【解决方案1】:

联合会为最大的成员分配空间。你有一个数组和一个指针,所以它会为数组分配空间。指针的大小是固定的,它指向什么并不重要。

当您使用malloc 时,空间并未在数据结构内分配,而是在内存中的其他位置。您将该内存位置分配给指针。

【讨论】:

  • 谢谢。我当时是怎么想的。换句话说,如果列表中有大量小数据成员,内存效率会很低。必须在这里重新考虑我的整个应用程序;P
猜你喜欢
  • 2014-08-05
  • 1970-01-01
  • 2020-02-09
  • 2013-02-05
  • 2014-10-06
  • 2019-07-19
  • 2020-07-31
  • 2015-06-24
  • 1970-01-01
相关资源
最近更新 更多