在PC和Arduino的C的++的sizeof()的区别 [英] Sizeof() difference between C++ on PC and Arduino
问题描述
可能重复:
<一href=\"http://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member\">Why为等于每个成员的sizeof的总和一个结构是不是和sizeof?
在以下code,值 structSize
取决于它的上一个Arduino VS我的电脑(Ubuntu的11.04 x64)的执行是不同的。
In the following code, the value of structSize
is different depending on whether it's executed on an Arduino vs my PC (Ubuntu 11.04 x64).
struct testStruct{
uint8_t val1;
uint16_t val2;
};
...
uint_8_t structSize = sizeof(testStruct);
在我的电脑, structSize
的值是4,和我的Arduino structSize
的值是3 (如预期)。
On my PC, the value of structSize
is 4, and on my Arduino the value of structSize
is 3 (as expected).
哪里是这4个字节来自哪里?
Where is this 4th byte coming from?
推荐答案
其实,我本来期望的大小为4,因为 uint16_t
通常对准到16位
Actually, I would have expected the size to be 4, because uint16_t
is usually aligned to 16 bits.
额外的字节填充插入成员之间保持对齐 uint16_t
。
The extra byte is padding inserted between the members to keep the alignment of uint16_t
.
这是编译器相关的,但。 Arduino的可能与内存更自私,可能不会很在乎对齐。 (可能的解释)
This is compiler dependent though. Arduino might be more selfish with memory and probably doesn't care that much about alignment. (possible explanation)
这篇关于在PC和Arduino的C的++的sizeof()的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!