+1

Why is the size of the memory of the struct variable 'place' is 16 Bytes?

Code: #include <stdio.h> struct residence{ char city[10];//10 int plz;//4 }; struct address{ char street[20];//20 int housenr;//4 struct residence place; }; int main() { struct residence place; printf("%d Bytes ",sizeof(place)); return 0; }

c

9/8/2019 7:36:32 AM

Preet

9 Answers

New Answer

+4

every thing is explained here https://www.google.com/amp/s/www.geeksforgeeks.org/is-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member/amp/

+3

Preet Is your struct residence, your struct place? Anyways, the size of declared members in struct residence is 14 bytes. But compilers usually allocate structs on integer boundary (means final sizeof (struct) % 4 == 0) or 4 byte boundary, for fast data access. Compiler will automatically pad the structure with required number of bytes (here 2) ✳AsterisK✳ It usually goes by sizeof (int) If your declared structure size is 9 bytes then the final size will be 12 not 16.

+3

✳AsterisK✳ Array as the only member is likely to have a same size as declared. Compiler may strip the structure part completely. if yoy have struct ABC { char a; }; going by usual rule the size should be 4, but the size will be 1, as there is only single char. Alignment comes in when there are more than 1 datatype. struct ABC { char a1; char a2; }; sizeof(ABC) = 2 not 4 struct ABC { char a; short b; }; declared size 3, actual size 4 struct ABC { char a1, a2, a3, a4, a5; }; declared size 5, actual size 5

+2

memory is arranged as a group of 8 byte on a 64 bits machine and 4 on a 32 bits machine this allows an increase in processing speed

+2

✳AsterisK✳ Don't have the exact answer. It's probably as a part of compiler optimization and less internal bookkeeping. Consecutive chars can be treated as arrays. for single char, why keep the overhead of struct. Compilers can do lots of optimization. (My answer could be wrong, don't rely on it :) )

+1

thanks ~ swim ~ but what about the array of size 10 with???

+1

thanks alot ~ swim ~

+1

✳AsterisK✳ Well i didn't gave you the proper answer but thanks accepted 😁

0

but why is it like that and not in the actual pack of 4??? ~ swim ~