Memory alignment in structures

Discussion in 'C Programming' started by rahul8143@gmail.com, Sep 26, 2005.

  1. Guest

    hello,
    What is mean by memory alignment in context of structure padding?
    also why structure padding is useful? is it implicitly done by latest
    c,c++ compilers or i have to specify it explicitly? how?
    also why there is differece for data type integer on MS-DOS as 2 bytes
    and on Windows as 4 bytes?
    why the same is not true for short data type?
     
    , Sep 26, 2005
    #1
    1. Advertising

  2. > What is mean by memory alignment in context of structure padding?

    Certain C types need to be aligned so that their addresses fall on
    a multiple of some number (often, but not always, their size) for
    hardware (you get an exception if you don't follow this rule) or
    efficiency (you might have to do two memory fetches instead of one)
    reasons. For example, 2-byte integers might need to be aligned on
    a multiple of 2, and 4-byte integers or pointers might need to be aligned
    on a multiple of 2 or 4, and 8-byte integers or doubles might need
    to be aligned on a multiple of 2, 4, or 8.

    >also why structure padding is useful?


    It makes the program run (as opposed to throwing smegmentation
    faults or some other kind of alignment error trap), or run faster,
    on the hardware you are using.

    >is it implicitly done by latest
    >c,c++ compilers or i have to specify it explicitly? how?


    There is no standard way to explicitly specify it. It's not padding
    if you specifically add an unused structure member, and doing so
    may not do what to you expect to the padding in the next release
    of the compiler, or on a different platform.

    >also why there is differece for data type integer on MS-DOS as 2 bytes
    >and on Windows as 4 bytes?


    Because the compiler writers can.

    >why the same is not true for short data type?


    Because the compiler writers decided not to.

    A lot of this has to do with the processor it's running on and how it
    is being used. MS-DOS has its roots in 8086 real mode. Windows
    uses more of the memory-mapping features of the processor and is
    designed around having a lot more memory available.

    Gordon L. Burditt
     
    Gordon Burditt, Sep 26, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alfonso Morra
    Replies:
    11
    Views:
    733
    Emmanuel Delahaye
    Sep 24, 2005
  2. Sandeep
    Replies:
    5
    Views:
    440
    Thomas Tutone
    Dec 4, 2005
  3. structures and alignment issues

    , Jun 14, 2007, in forum: C Programming
    Replies:
    31
    Views:
    812
    Keith Thompson
    Jun 16, 2007
  4. Replies:
    20
    Views:
    726
    Richard
    Aug 10, 2007
  5. mathog

    Data alignment questin, structures

    mathog, Jan 12, 2013, in forum: C Programming
    Replies:
    46
    Views:
    601
    Keith Thompson
    Jan 22, 2013
Loading...

Share This Page