2

When I run following, it gives me output as 20. but int is of 4 byte,float is of 4 byte and character array is of 10 byte, then the total is 18 byte. Why I am getting output as 20 byte?

#include<stdio.h>

struct emp
{
    int id;
    char name[10];
    float f;
}e1;
main()
{
    printf("\n\tSize Of Structure is==>%d\n",sizeof(e1));
}
talonmies
  • 70,661
  • 34
  • 192
  • 269
Amrut Dange
  • 191
  • 1
  • 6

5 Answers5

4

For several reasons (instruction set architecture, performance of generated code, ABI conformance...), the compiler is doing some data padding.

You could perhaps use the GCC __attribute__((packed)) (if using GCC or a compatible compiler like CLANG) to avoid that (at the risk of slower code and non-conformance with called libraries). In general avoid packing your data structure, and if possible order fields in them by decreasing alignment. See also __alignof__ in GCC.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
4

It is address alignment.

See :

int 4

char 10 ==> 12 for alignment

float 4

like :

iiii
cccc
cccc
cc^^  <-- a data pading to make address alignment.
ffff

total 20

Lidong Guo
  • 2,817
  • 2
  • 19
  • 31
2

First of all, it would be 18, not 16. Secondly, the compiler is allowed to add padding to structs, usually for alignment requirements for the platform in question.

zindorsky
  • 1,592
  • 9
  • 9
1

What is going on is called data structure alignment, or commonly discussed in two closely related parts: data alignment and data padding.

For the processor to be able to read the bytes it needs to be set as a memory offset equal to the some multiple of the word size chunk (the word size chunk is often the amount of bytes required to store an integer), this is known as data alignment. Data padding is the process of inserting random bytes to have a proper offset with a multiple of the word size chunk. This can be done in the middle or at the end of a structure, entirely up to the compiler.

Consider the following example on a 32-bit environment. Looking at your structure:

struct emp {
    int id;
    char name[ 10 ];
    float f;
};

If you were to create a new structure, it could be seen in memory as follows:

1. (byte for integer)
2. (byte for integer)
3. (byte for integer)
4. (byte for integer)
5. (byte for char)
6. (byte for char)
7. (byte for char)
8. (byte for char)
9. (byte for char)
10. (byte for char)
11. (byte for char)
12. (byte for char)
13. (byte for char)
14. (byte for char)
15. ***(padding byte)***
16. ***(padding byte)***
17. (byte for float)
18. (byte for float)
19. (byte for float)
20. (byte for float)

Remark:

[x] It can store an integer without any padding.

[x] It can store the 10 bytes for an array of 10 characters.

Notice that the amount of bytes for the first two fields has tallied to 14 bytes, which is not a multiple of the word size chunk 4. The compiler then inserts the proper offset of bytes.

[x] It stores two random bytes that are used to offset 14 and 4.

[x] It stores four bytes for a float.

... and hence the amount of bytes required for the emp structure is 20 bytes (rather than the initial thought of 18). The compilers are trading performance for space efficiency.

Jacob Pollack
  • 3,703
  • 1
  • 17
  • 39
0

Here char is taking 2 bytes. This is done to reduce the access time for that variable. Now how does that reduce access time : read about memory bank i.e even bank and odd bank concept. If you want to make char take one byte use #pragma pack. But before doing so take a look at this

Community
  • 1
  • 1
Anirudha Agashe
  • 3,510
  • 2
  • 32
  • 47