CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: memory alignment of a struct

  1. #1
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    memory alignment of a struct

    Hello everyone,


    I heard when define a struct, it is important to make the size of the struct memory aligned.

    For example,

    Code:
    struct foo
    {
        char p;
        char[3] align;
    };
    Code:
    struct goo
    {
        char p;
    };
    struct foo is better than struct goo, even if we only use member p.

    Allocate 3 bytes to align with memory. But I do not know why make memory aligned is better. Any reasons? (for example, prevent memory from running out?)


    thanks in advance,
    George

  2. #2
    Join Date
    Jul 2001
    Location
    Otaki, New Zealand
    Posts
    303

    Re: memory alignment of a struct

    Depends what you're doing with the struct I guess. For your goo example, a compiler is likely to allocate 4 or 8 bytes for this structure even though it technically only needs 1 byte (ignoring vtables etc). You can alter the alignment using pragmas if you need to do something like overlaying the structure on an area of memory (or raw bytes) or to address a hardware device.

    For most applications though I personally don't worry too much about the alignment.

    Regards
    Alan

  3. #3
    Join Date
    Feb 2005
    Location
    Pune (India)
    Posts
    644

    Re: memory alignment of a struct

    Hi,

    Its easy and fast for accessing/fetching memory which is aligned to system words/bus size from hardware perspective i.e CPU

    thats why memory alignment is used !

    for unaligned memory CPU needs extra cycles for accessing/fetching.

    -Anant
    "Devise the simplest possible solution that solves the problems"

  4. #4
    Join Date
    Feb 2005
    Location
    Pune (India)
    Posts
    644

    Thumbs up Re: memory alignment of a struct

    "Devise the simplest possible solution that solves the problems"

  5. #5
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: memory alignment of a struct

    Hi Anant,


    Quote Originally Posted by anantwakode
    Hi,

    Its easy and fast for accessing/fetching memory which is aligned to system words/bus size from hardware perspective i.e CPU

    thats why memory alignment is used !

    for unaligned memory CPU needs extra cycles for accessing/fetching.

    -Anant
    I do not know why alignment will make memory reading operation faster. In my case, foo needs to fetch 4 bytes and goo needs to fetch 1 byte. Even if each time, CPU fetch 4 bytes, I think they should be the same performance, right?

    Why do you think memory alignment will improve performance? An example?


    regards,
    George

  6. #6
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: memory alignment of a struct

    Thanks Alan,


    Quote Originally Posted by AlanGRutter
    Depends what you're doing with the struct I guess. For your goo example, a compiler is likely to allocate 4 or 8 bytes for this structure even though it technically only needs 1 byte (ignoring vtables etc). You can alter the alignment using pragmas if you need to do something like overlaying the structure on an area of memory (or raw bytes) or to address a hardware device.

    For most applications though I personally don't worry too much about the alignment.

    Regards
    Alan
    I am interested in two points,

    1. How to do memory overlapping?

    2. How to address a H/W device?

    You mean both of them could be implemented by #pragma? Could you show an example please?


    regards,
    George

  7. #7
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: memory alignment of a struct

    Thanks Anant. It is a good article. But I do not understand why alignment will improve performance. Could you show an example please?


    Quote Originally Posted by anantwakode

    regards,
    George

  8. #8
    Join Date
    Feb 2005
    Location
    Pune (India)
    Posts
    644

    Thumbs up Re: memory alignment of a struct

    Hi,

    e.g

    struct foo
    {
    char ch1;
    };

    struct soo
    {
    char ch2;
    };

    see if I create objects
    foo f;
    soo s;

    memory structure...may be like this

    | f.ch1,s.ch2 | | |

    may some cpu instructions have to filterout/Ignore (will need extra work to do to leave s.ch2 untouched ) s.ch2 while processing f.ch1 as the most of the instructions are developed to read/work on words at a time


    if these are aligned

    |f.ch1 |s.ch2 | |

    can be fetched and processed seprately .................



    -Anant
    "Devise the simplest possible solution that solves the problems"

  9. #9
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: memory alignment of a struct

    Quote Originally Posted by anantwakode
    Hi,

    e.g

    struct foo
    {
    char ch1;
    };

    struct soo
    {
    char ch2;
    };

    see if I create objects
    foo f;
    soo s;

    memory structure...may be like this

    | f.ch1,s.ch2 | | |

    may some cpu instructions have to filterout/Ignore (will need extra work to do to leave s.ch2 untouched ) s.ch2 while processing f.ch1 as the most of the instructions are developed to read/work on words at a time


    if these are aligned

    |f.ch1 |s.ch2 | |

    can be fetched and processed seprately .................



    -Anant
    What is your format mean? Like sign '|' ??

    | f.ch1,s.ch2 | | |

    |f.ch1 |s.ch2 | |


    regards,
    George

  10. #10
    Join Date
    Feb 2005
    Location
    Pune (India)
    Posts
    644

    Thumbs up Re: memory alignment of a struct

    hi,

    | means Word Boundry, i.e memory space

    |1000|1001|1002|1003|..................
    |Word|Word|Word|Word|Word|Word|Word|Word|Word|Word|


    -Anant
    "Devise the simplest possible solution that solves the problems"

  11. #11
    Join Date
    Jul 2001
    Location
    Otaki, New Zealand
    Posts
    303

    Re: memory alignment of a struct

    What is being said about alignment is true however you generally do not need to specifically pad out your data structures so that they align on word boundaries as the compiler will do this for you.

    Consider the following code

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    struct goo
    {
        char p;
        int i;
    };
    
    #pragma pack(1)
    
    struct goo1
    {
        char p;
        int i;
    };
    
    #pragma align()
    
    int main()
    {
        // character 'A' + Integer value 7979 (note byte ordering)
        char bytes[] = {0x41, 0x2b, 0x1f, 0x00, 0x00}; 
    
        cout << "Sizeof char: " << sizeof(char) << endl;
        cout << "Sizeof int: " << sizeof(int) << endl;
    
        cout << "Sizeof unpacked structure goo: " << sizeof(goo) << endl;
        cout << "Sizeof packed structure goo1: " << sizeof(goo1) << endl;
    
        //Overlay goo1 onto byte array
        goo1 *pStructGoo1 = reinterpret_cast<goo1 *>(bytes);
    
        cout << "Char p: " << pStructGoo1->p << endl;
        cout << "Integer i: " << pStructGoo1->i << endl;
    
        return 0;
    }
    In the first instance (goo) the compiler has allocated 4 bytes for the char and 4 bytes for the int. If I use the pragma to pack the structure to single byte boundaries, the extra space is removed.

    By packing to single byte boundaries you can overlay a structure onto a stream of bytes as demonstrated above.

    Regards
    Alan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)