Memory Alignment

Discussion in 'C Programming' started by Dharma, Nov 13, 2005.

  1. Dharma

    Dharma Guest

    hi,

    struct a
    {
    byte d;
    byte buf[16];
    }text;

    text.buf[0] = 'A'
    text.buf[1] = '\0'
    text.buf[2] = 'B'
    text.buf[3] = '\0'
    text.buf[4] = '\0'
    text.buf[5] = '\0'

    main()
    {
    unsigned short *c = NULL;
    c = (unsigned short*)&text.buf;
    if(0x0000 == *c)
    {
    return ;
    }
    }

    i have a structure as above. Iam type casting the byte to unsigned
    short pointer.
    when i do (0x0000 == *c) the first byte becomes 0x00. I found that buf
    starts with odd memory address, If i make the memory Alignment to even
    there is no problem,
    What the low level does if memory Alignment is odd aligned and it is
    unsigned short* type.

    thanks,
    Dharma.
    Dharma, Nov 13, 2005
    #1
    1. Advertising

  2. "Dharma" <> writes:
    > struct a
    > {
    > byte d;
    > byte buf[16];
    > }text;


    C has no standard type called "byte".

    > text.buf[0] = 'A'
    > text.buf[1] = '\0'
    > text.buf[2] = 'B'
    > text.buf[3] = '\0'
    > text.buf[4] = '\0'
    > text.buf[5] = '\0'
    >
    > main()


    This should be "int main(void)".

    > {
    > unsigned short *c = NULL;
    > c = (unsigned short*)&text.buf;
    > if(0x0000 == *c)
    > {
    > return ;
    > }
    > }
    >
    > i have a structure as above. Iam type casting the byte to unsigned
    > short pointer.


    No, you're converting an address of (i.e., a pointer to) a byte to a
    pointer to an unsigned short.

    > when i do (0x0000 == *c) the first byte becomes 0x00. I found that buf
    > starts with odd memory address, If i make the memory Alignment to even
    > there is no problem,
    > What the low level does if memory Alignment is odd aligned and it is
    > unsigned short* type.


    Alignment requirements vary from one system to another. If type short
    requires even alignment, and you have a misaligned pointer, attempting
    to dereference it (as in *c) invokes undefined behavior. As far as
    the language is concerned, anything could happen.

    The solution: don't do that.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Nov 13, 2005
    #2
    1. Advertising

  3. Dharma

    Old Wolf Guest

    Keith Thompson wrote:

    > "Dharma" <> writes:
    >> struct a
    >> {
    >> byte d;
    >> byte buf[16];
    >> }text;
    >> main()
    >> {
    >> unsigned short *c = NULL;
    >> c = (unsigned short*)&text.buf;
    >> if(0x0000 == *c)

    >
    > Alignment requirements vary from one system to another. If type short
    > requires even alignment, and you have a misaligned pointer, attempting
    > to dereference it (as in *c) invokes undefined behavior. As far as
    > the language is concerned, anything could happen.


    I think the cast causes undefined behaviour too, if there is an
    alignment problem.
    Old Wolf, Nov 13, 2005
    #3
  4. "Old Wolf" <> writes:
    > Keith Thompson wrote:
    >> "Dharma" <> writes:
    >>> struct a
    >>> {
    >>> byte d;
    >>> byte buf[16];
    >>> }text;
    >>> main()
    >>> {
    >>> unsigned short *c = NULL;
    >>> c = (unsigned short*)&text.buf;
    >>> if(0x0000 == *c)

    >>
    >> Alignment requirements vary from one system to another. If type short
    >> requires even alignment, and you have a misaligned pointer, attempting
    >> to dereference it (as in *c) invokes undefined behavior. As far as
    >> the language is concerned, anything could happen.

    >
    > I think the cast causes undefined behaviour too, if there is an
    > alignment problem.


    You're right. C99 6.3.2.3p7:

    A pointer to an object or incomplete type may be converted to a
    pointer to a different object or incomplete type. If the resulting
    pointer is not correctly aligned for the pointed-to type, the
    behavior is undefined.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Nov 14, 2005
    #4
  5. > i have a structure as above. Iam type casting the byte to unsigned
    > short pointer.
    > when i do (0x0000 == *c) the first byte becomes 0x00. I found that buf
    > starts with odd memory address, If i make the memory Alignment to even
    > there is no problem,
    > What the low level does if memory Alignment is odd aligned and it is
    > unsigned short* type.


    The following article should help:

    http://www.eventhelix.com/RealtimeMantra/ByteAlignmentAndOrdering.htm

    --
    EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
    Sequence Diagram Based Real-time and Embedded System Design Tool
    EventHelix.com, Nov 14, 2005
    #5
    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. Peter van Merkerk

    Re: memory alignment?

    Peter van Merkerk, Aug 1, 2003, in forum: C++
    Replies:
    1
    Views:
    387
    David Cattarin
    Aug 1, 2003
  2. Thomas Matthews

    Re: memory alignment?

    Thomas Matthews, Aug 1, 2003, in forum: C++
    Replies:
    0
    Views:
    369
    Thomas Matthews
    Aug 1, 2003
  3. Andrew Koenig

    Re: memory alignment?

    Andrew Koenig, Aug 1, 2003, in forum: C++
    Replies:
    1
    Views:
    322
    Karl Heinz Buchegger
    Aug 1, 2003
  4. J. Campbell
    Replies:
    14
    Views:
    495
    Default User
    Oct 9, 2003
  5. Paul_Huang

    Memory Padding and alignment

    Paul_Huang, Sep 21, 2004, in forum: C++
    Replies:
    2
    Views:
    3,950
    Serge Paccalin
    Sep 21, 2004
Loading...

Share This Page