0

I've found a code for microcontroller with a strange nesting of structures and union.

What is the purpose or optimizations of this kind of structure:

struct sExtTable {
        ... data
        struct {
            ... data
            union {
                struct {
                    ... data
                } intUnionStruct;
                struct {
                    ... data
                } intUnionStruct2;
            } union;
        } intStruct;
} ExtTable;

I access this data as ExtTable.intStruct.union.intUnionStruct.data . Why a union instead leaving the two struct (intUnionStruct1 and intUnionStruct2) inside intStruct ?

Is there some kind of memory optimization?

I'm sorry to not be able to load code since is not public.

Singed
  • 141
  • 3
  • Not a complete duplicate, but https://stackoverflow.com/questions/346536/difference-between-a-structure-and-a-union has some info. What are you asking? Why use a union, or why is it inside a struct? – doctorlove May 15 '23 at 13:32
  • The reason for the layout is impossible to even guess without some context. Where did you get this from? Can't you ask the author of the code? – Some programmer dude May 15 '23 at 13:33
  • We do not have enough information to speak to the purpose of this data structure design in the particular code you are looking at. Generally speaking, though, a `union` stores only one of its members at a time, and occupies space determined mostly by the size of its largest member. A structure, on the other hand, occupies space determined by all of its members. – John Bollinger May 15 '23 at 13:34
  • Unions for microcontroller register maps are typically used to allow various forms of access of the same memory location. Byte vs word etc. – Lundin May 15 '23 at 14:26

1 Answers1

0
        union {
            struct {
                ... data
            } intUnionStruct;
            struct {
                ... data
            } intUnionStruct2;
        } union;

Is there some kind of memory optimization?

Yes, intUnionStruct and intUnionStruct2 share the same memory location so the memory allocated will be as big as the larger of those structs. If you have both structs not in the union, then the memory will be allocated for both of them.

Example:


struct x
{
    union
    {
        struct
        {
            int a;
            float b;
            double c;
        }struct1;
        struct
        {
            int a;
        }struct2;
    };
};

struct y
{
    struct
    {
        int a;
        float b;
        double c;
    }struct1;
    struct
    {
        int a;
    }struct2;
};

int main(void)
{
    printf("%u %u\n", sizeof(struct x), sizeof(struct y));
}

The result:

16 24
0___________
  • 60,014
  • 4
  • 34
  • 74
  • Understood how is memorized. But when I make an union between variables, all variables are updated when I assign a value to one of the variable inside this union. In this case this two structs contains each different variables with different names. I suppose that I can populate a single struct each time and they are exclusive right, otherwise, if I put some data in one struct and some in another, I'll overwrite some data of one structure and some of another. My assumption is right? – Singed May 18 '23 at 07:28