Thread: memory alignment of a struct

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    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
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by George2 View Post
    I heard when define a struct, it is important to make the size of the struct memory aligned.
    Then look into your compiler's documentation of how to assure memory alignment. Such things are non-standard.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    The computer has to retrieve memory starting at a word-aligned address. Modern compilers automagically do this alignment for you unless you specifically give the compiler-specific preprocessor directive to pack it. On some architectures it can actually create a SIGBUS error if your structure isn't word-aligned.

    Feel free to correct any mistakes I've made in the above statements. I think everything is accurate but I'm not 100% sure.
    If you understand what you're doing, you're not learning anything.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > struct foo is better than struct goo, even if we only use member p.
    Not so.
    Manually aligning structures is a waste of time. The compiler will do it for you if it's necessary to do it, and it will do it with the right number of bytes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. problem with sending array of struct over shared memory
    By jet-plane in forum C Programming
    Replies: 26
    Last Post: 05-10-2008, 04:10 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 10:24 AM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM