I wrote a function in file example1.c as below:

void function(int a)
{
    char buffer1[1];
}

When used "gcc -S -o example1.s example1.c, I got the following assembly code:

_function:
pushl %ebp
movl %esp, %ebp
subl $4, %esp
leave
ret

And if I change buffer1[1] to buffer1[2] or buffer1[4], the assembly code is the same. However, when I wrote buffer1[3] instead of buffer1[1], I got the following assembly code:

_function:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
leave
ret

I don't know why. Who can tell me the reason? Thank you.
My PC:
CPU:AMD Turion 64 X2
Memory: DDR2 667
OS:Windows XP Home SP3

Looks like it is being stupid about alignment?

Looks like it is being stupid about alignment?

Yeah, I really get confused about alignment. I have read some articles about alignment but still couldn't find the answer.

A structure is 'aligned' in memory to make it easy to access quickly. Search your compiler's documentation for "record alignment".

Hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.