J Cooper
J Cooper

Reputation: 17051

Simple struct alignment/padding question

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::string 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...

Upvotes: 0

Views: 130

Answers (1)

Ray Toal
Ray Toal

Reputation: 88378

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.

Upvotes: 2

Related Questions