What makes a UU look like a memory?

enum Enums { k1, k2, k3, k4 };

union MYUnion { 
    struct U{ 
         char P;
    }u;

    struct U0 { 
        char state; 
    } u0; 

    struct U1 { 
        Enums e; 
        char c; 
        int v1; 
    } u1; 

    struct U2 { 
        Enums e; 
        char c; 
        int v1; 
        int v2; 
    } u2; 

    struct U3 { 
        Enums e; 
        unsigned int i; 
        char c; 
    } u3; 

    struct U4 { 
        Enums e;
        unsigned int i; 
        char c; 
        int v1; 
    } u4; 

    struct U5 { 
        Enums e; 
        unsigned int i; 
        char c; 
        int v1; 
        int v2; 
    } u5; 
} myUnion

I am so confused by this idea of ​​Union in C ++. What does this "myUnion" look like in memory? I know that data has the same memory block, but how? What is the size of "MyUnion"? If this is the size of "u5", then how is the data distributed in this memory block?

+3
source share
5 answers
  • union size is the size of the largest thing in the union.
  • The location of the pool is what you last saved.

So, in your last union, if you store in .iand then store in .e, the first byte of int will be overwritten with the value enum (assuming it sizeof (enum)is 1 in your environment).

The union looks like:

void * p = malloc(sizeof(biggest_item))
Enums *ep = (Enums *)e;
unsigned int * ip = (unsigned int *)p;
char *cp = (char *)p;

*ep, *ip *cp , .

+1

, , .

. :

:

union Foo
{
  struct A
  {
    int x;
    unsigned char y;
    unsigned char z;

  }
  struct B
  {
    unsigned char a;
    unsigned char b;
    unsigned char c;
    unsigned char d;
    unsigned char e;
  }
}

, , int - 32 ( ), a, b, c d , X. x, b x ..

, X a, b, c d.

y e ( , , int - 32 ), .y .e .

char A.z B, B.

, . , .

+1

, ! ... - , .

. , , ( f) . int . , , , , .

union AUnion
{
   int i;
   float f;
}; // assumes an int is 32 bits

AUnion aUnion;
aUnion.i = 0;
printf("%f", aUnion.f);

, ? , , ints float . 32 . . aUnion.i = 0, " 0'd aUnion". 0'd , , 32- 0. , aUnion.f, : " aUnion, 32- ! , float, int. , 32 float, , .

, :

enum Enums { k1, k2, k3, k4 };

union MYUnion { 
struct U{ 
     char P;
}u;

struct U0 { 
    char state; 
} u0; 

struct U1 { 
    Enums e; 
    char c; 
    int v1; 
} u1; 

, int float. , , int. int , :

 enum Enums { k1/*0*/, k2/*1*/, k3/*2*/, k4/*3*/ };

, , ,

union MYUnion { 
struct U{ 
     char P;
}u;

struct U0 { 
    char state; 
} u0; 

struct U1 { 
    int e; 
    char c; 
    int v1; 
} u1; 

,

MyUnion m;
m.u.P = 'h'

(, , ), . , P 1 , int - 4 . , .

, .

+1

(-) .. , , , - , , , .

0

, . , . unportable ( ). .

.

enum mytags { FULL_TIME_WORKER, PART_TIMER, CONTRACTOR}

struct worker_t {
    enum mytags tag,
    union {
        struct full_time_worker_t full_time_wroker,
         struct part_time_worker_t part_time_worker,
         struct contractor_t contractor }
    }
}


bool person_can_do_x(worker_t w)
{
    switch(workers.tag)
    {
    case FULL_TIME_WORKER:
    ...
    case PART_TIMER:
    ...
    case CONTRACTOR:
       ...w.contractor.....
    default:
        return true
    }
}
0

Source: https://habr.com/ru/post/1723573/


All Articles