Understanding stack allocation and alignment

前端 未结 5 1578
一生所求
一生所求 2021-01-05 19:54

I\'m trying to understand how stack alignment works as described in what is "stack alignment"? but I have trouble getting a small example to demonstrate the said b

相关标签:
5条回答
  • 2021-01-05 19:54

    You can check how extra memory is allocated for your data structure using a tool called pahole http://packages.debian.org/lenny/dwarves . It shows you all the holes of your program: the size of your data if you sum it up and the real size allocated at your stuck

    0 讨论(0)
  • 2021-01-05 19:55

    The usual rule is that variables are allocated on 32-bit boundaries. I'm not sure why you think 16 bytes has any special meaning.

    0 讨论(0)
  • 2021-01-05 20:01

    I think you're missing the fact that there is no requirement for all stack variables to be individually aligned to 16-byte boundaries.

    0 讨论(0)
  • A good example is to see this on a structure.

    struct a{
        int a;
        char b;
        int c;
    } a;
    

    On a 32 bit system this would be 4+1+4 bytes if taken separately.

    Because the structure and it's members are aligned "char b" will be 4 bytes, taking this to 12 bytes.

    struct b{
        int a;
        char b;
        int c;
    } __attribute__((__packed__)) b;
    

    Using the packed attribute you can force it to keep it's minimum size. Thus this structure is 9 bytes.

    You can check this as well http://sig9.com/articles/gcc-packed-structures

    Hope it helpes.

    0 讨论(0)
  • 2021-01-05 20:08

    I've never heard about such a thing as specific stack alignment. If there is alignment requirements for the CPU, alignment is done on all kinds of data memory, no matter if it is stored on the stack or elsewhere. It is starting on an even addresses with 16, 32 or 64 bit of data following.

    16 bytes may perhaps be some sort of on-chip cache memory optimization, though that seems a bit far-fetched to me.

    0 讨论(0)
提交回复
热议问题