Click here to Skip to main content
15,569,234 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include <stdio.h>

struct test
{
    unsigned char first;
    int second;
    unsigned char three;
    struct test *fourth;

};

int main()
{
    struct test node;
    printf("%d\n", sizeof(node));
    return 0;
}


What I have tried:

In the above code the size of the structure in 64 bit processor should be 20 but why am I getting answer 24? can any one explain please.
Posted
Updated 4-Dec-22 21:03pm
Comments
Rick York 19-Dec-20 13:26pm    
Look up the offsetof or offset_of macro. It can helpful for displaying this information and seeing where the padding is inserted.

64 bit alignment: 4 + 4 + 4 + (4) + 8.
 
Share this answer
 
Comments
Padala Vamsi ujpNQUXGRi 19-Dec-20 11:14am    
why there is an additional 4 bytes. can you please explain?
Padala Vamsi ujpNQUXGRi 19-Dec-20 11:16am    
let say the alignment is

e e e 1
2 2 2 2
e e e 3
4 4 4 4
4 4 4 4

where e stands for empty or padding and 1, 2, 3, 4 represents variables. Now there is only 20 bytes of memory used.
CPallini 19-Dec-20 13:25pm    
5.
A processor always aligns items to "natural" boundaries: so a 32 bit integer will always start on an address that is divisible by 32 / 8 for example (32 bits, 8 bits per byte - so the address of the integer will always be divisible by 4 with no remainder).
So your first element takes up one byte - but the next item needs to be aligned to a 32 bit boundary, so three "padding bytes" are added to get there.
The same thing happens with your three element - it's followed by a pointer so more padding bytes are added to get to a 64 bit boundary.
So:
Address  value
00       first  (8 bit byte)
01       padding
02       padding
03       padding
04       second (32 bit int)
05       second
06       second
07       second
08       three  (8 bit byte)
09       padding
10       padding
11       padding
12       padding
13       padding
14       padding
15       padding
16       fourth (64 bit pointer)
17       fourth (64 bit pointer)
18       fourth (64 bit pointer)
19       fourth (64 bit pointer)
20       fourth (64 bit pointer)
21       fourth (64 bit pointer)
22       fourth (64 bit pointer)
23       fourth (64 bit pointer)
Total: 24 bytes.
To check it, swap second and three and it needs less padding:
C++
struct test
{
    unsigned char first;
    unsigned char three;
    int second;
    struct test *fourth;

};

Address  value
00       first  (8 bit byte)
08       three  (8 bit byte)
02       padding
03       padding
04       second (32 bit int)
05       second
06       second
07       second
08       fourth (64 bit pointer)
09       fourth (64 bit pointer)
10       fourth (64 bit pointer)
11       fourth (64 bit pointer)
12       fourth (64 bit pointer)
13       fourth (64 bit pointer)
14       fourth (64 bit pointer)
15       fourth (64 bit pointer)
Total: 16 bytes.

In fact, if you reorder them properly, you can fit all of your elements plus another value into the same space:
C++
struct test
{
    struct test *fourth;
    int second;
    unsigned char first;
    unsigned char three;
    short five;
};
Total: 16 bytes
 
Share this answer
 
v3
Comments
Padala Vamsi ujpNQUXGRi 19-Dec-20 11:31am    
Thank you very much.
OriginalGriff 19-Dec-20 12:06pm    
You're welcome!
Padala Vamsi ujpNQUXGRi 19-Dec-20 12:03pm    
I tried printing addresses of variables using

printf("first(%p) second(%p) third(%p) fourth(%p)\n", &node.first, &node.second, &node.three, &node.fourth);

what I got was

first(0x7ffc0f5f4740) second(0x7ffc0f5f4744) third(0x7ffc0f5f4748) fourth(0x7ffc0f5f4750)

that means

Address value
40    first
41    padding
42    padding
43    padding
44    second
45    second
46    second
47    second
48    three
49    padding
50    fourth
51    fourth
52    fourth
53    fourth
54    fourth
55    fourth
56    fourth
57    fourth

I don't understand how it got padding.
OriginalGriff 19-Dec-20 12:06pm    
One word: Hexadecimal.
Padala Vamsi ujpNQUXGRi 19-Dec-20 12:07pm    
Ok, funny mistake. Thank you and sorry to bother you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900