Size of type and memory allocation
This question already has an answer here:
actual size of my struct (sum of all the parts) is less than size of the type itself"
Because many computer operations are faster when the values they work on are aligned at certain memory boundaries, the C language standard allows implementations to do such alignment. On most systems, ints will be aligned on 4- or 8- byte boundaries, depending on the size of the int. The size of a struct must be aligned so that the address of the next struct in an array will be properly aligned, so a struct starting with an int aligned to a 4 byte boundary must have a size that is a multiple of 4.
Does this mean i lose 3 bytes on nothing?
Yes. (Well, it's not exactly nothing ... your program will generally be faster because of it.) With modern machines supporting up to 2 terabytes of RAM, this is less of a concern than it once was.
链接地址: http://www.djcxy.com/p/14072.html上一篇: 如何知道内存分配给引用和值类型?
下一篇: 类型和内存分配的大小