ç位域的内存使用情况 [英] C bitfields memory usage
问题描述
我需要在下面的表格来处理一些数据:
I need to deal with some data in the following form:
typedef struct{
unsigned n1 : 12;
unsigned n2 : 12;
unsigned n3 : 12;
unsigned n4 : 1;
unsigned n5 : 35;
} data;
我确信,在他们总共数到9个字节。结果
但他们不..写9个字节,该结构的一个文件并读不会恢复所有的数据和的sizeof(数据)
返回16. < BR>
有什么问题吗?
I made sure that in total they count up to 9 bytes.
But they don't.. Writing 9 bytes of that struct to a file and reading it back doesn't restore all the data, and sizeof(data)
returns 16.
What's the problem here ?
推荐答案
问题是一些填充正由编译器效率的原因说。
The problem is some padding is being added by the compiler for efficiency reasons.
此行为可以被覆盖。
This behavior can be overridden.
有关如何做到这一点用gcc看到迫使GCC对齐
For how to do this with gcc see forcing alignment in GCC
有关如何做到这一点在Visual C ++,请参阅:迫使排列在Visual C ++
For how to do this with visual c++ see: forcing alignment in Visual C++
这篇关于ç位域的内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!