开发者

Simple struct alignment/padding question

开发者 https://www.devze.com 2023-03-25 07:44 出处:网络
I have a 32-bit C++ program here, and was checking disassembly out of curiosity. I have the following struct:

I have a 32-bit C++ program here, and was checking disassembly out of curiosity. I have the following struct:

struct Blah
{
    char foo;
    std::strin开发者_开发技巧g bar;
};

When accessing blah->bar, I noted the following:

; ... address of blah is in eax
003A227B  add         eax,4  

So my question is, why does 4 needed to be added to blah's address to get to bar? I would understand 3, because char is 1 byte and that would make a nice round 4...


It is adding 4 to the base address of blah. I guess that is the same as adding 3 to the address just after foo. :)

It is indeed an alignment issue. Pointers should be aligned on address which are a multiple of 4.

0

精彩评论

暂无评论...
验证码 换一张
取 消