具有联合静态和动态字符数组的结构

时间:2021-03-02 19:58:33

I'm creating a dynamically allocated linked list where each item has a data element. Most of these elements are fixed size char arrays but some can be of varying length, E.g. only 1 or 2 chars - or 1000+.

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

If I use a union inside the data element with static and dynamic char array – is it always going to occupy:

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

  • size of the static one or if malloc'd the size of the malloced one?
  • 静态的尺寸还是malloc的尺寸?
  • size of the static one + if malloc'd the size of the malloced one?
  • 静态的尺寸+如果malloc的尺寸是错误的尺寸?
  • the size of the pointer to the malloced one or the size of the static one?
  • 指向错误定位的指针的大小还是静态指针的大小?
  • ... (and so on)
  • …(等等)

A simplified example without the list. This is not how I'm filling it with data, - only as an example of the struct union.

没有列表的简化示例。这不是我用数据填充它的方式,只是作为一个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: One of the whacky ways I tested:

@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;
}

1 个解决方案

#1


1  

A union will allocate space for largest member. You've got an array and a pointer, so it'll allocate space for the array. A pointer is fixed in size, it doesn't matter what it points to.

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

When you use malloc the space isn't allocated inside a data structure, it's somewhere else in memory. You assign that memory location to the pointer.

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

#1


1  

A union will allocate space for largest member. You've got an array and a pointer, so it'll allocate space for the array. A pointer is fixed in size, it doesn't matter what it points to.

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

When you use malloc the space isn't allocated inside a data structure, it's somewhere else in memory. You assign that memory location to the pointer.

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

相关文章