Memory Allocation for Structure Elements

Hey. I am having difficulty understanding how memory is allocated to structural elements.

For example, if I have a lower structure and the size of char is 1 and int is 4 bytes respectively.

struct temp
{
char a;
int b;
};

I know that the size of the structure will be 8. Since 3 bytes will be added after the char, and the next element should be placed in several of 4, so the size will be 8.

Now consider the structure below.

struct temp
{
int a;     // size is 4
double b;  // size is 8
char c;    // size is 4
double d;  // size is 8
int e;     // size is 4
};

This is o / p I got for the above structure

size of node is 40
the address of node is 3392515152 ( =: base)
the address of a in node is 3392515152 (base + 0)
the address of b in node is 3392515160 (base + 8)
the address of c in node is 3392515168 (base + 16)
the address of d in node is 3392515176 (base + 24)
the address of e in node is 3392515184 (base + 32)

Total memory up to 36 bytes, why is it displayed as 40 bytes? If we create an array of such a structure, the first element of the next element of the array can be placed in 3392515188 (base + 36), since it is a multiple of 4, but why does it not happen this way?

Can anyone plz solve my doubts.

, Saravana

+4
7

, double 8.

struct temp {
    int a;     // size is 4
    // padding 4 bytes
    double b;  // size is 8
    char c;    // size is 1
    // padding 7 bytes
    double d;  // size is 8
    int e;     // size is 4
    // padding 4 bytes
}; 
// Total 4+4+8+1+7+8+4+4 = 40 bytes

4 , , array[1].b .

( 0):

&array[0]   == 0
&array[1]   == 36
&array[1].b == 36 + 8 == 44  
44 % 8 == 4  ->  ERROR, not aligned!

( 0):

&array[0]   == 0
&array[1]   == 40
&array[1].b == 40 + 8 == 48
48 % 8 == 0  ->  OK!

, , paddings .

+4

, e :

0       8       16      24      32
AAAAaaaaBBBBBBBBCcccccccDDDDDDDDEEEEeeee

uppercase - , - , .

( ), 8 , .

, , e.

0

, . , .

, . , . , ( ) .

, , , . . double, .

0

, . "", 8- .

, , .

struct temp
{
double b;  // size is 8
int a;     // size is 4
int e;     // size is 4
double d;  // size is 8
char c;    // size is 4
}

, . . ARM, , [ "" , .

0

! #pragma pack ( 1 ) .

#pragma pack(1)
struct temp
{
int a;     // size is 4
int b;     // size is 4
double s;  // size is 8
char ch;   //size is 1
};

: 17

0

:

typedef struct structc_tag {   char c; ``   double d;   int s; } structc_t;

, structc_t sizeof (char) + 7 padding + sizeof (double) + sizeof (int) = 1 + 7 + 8 + 4 = 20 . sizeof (structc_t) 24 . , . . , structc_t, structc_t structc_array [3];

, structc_array 0 × 0000 . structc_t 20 (0 × 14) , , structc_t ( 1) 0 × 0000 + 0 × 0014 = 0 × 0014. 1 . structc_t 0 × 0014 + 0 × 1 + 0 × 7 = 0x001C ( 28), 8 double. , double 8 . , . . structa_t 2, structb_t 4, structc_t 8. , .

structc_t 4 int, . sizeof (structc_t) 24 . .

0

, 3392515188 ( + 36), 4, ?

- double.

, , double . , char c .

, . double , . , int sizeof(temp) .

, . , double , double , .

0

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


All Articles