using member address of an unaligned structure

Discussion in 'C Programming' started by Steven Woody, Feb 8, 2006.

  1. Steven Woody

    Steven Woody Guest

    hi,

    i get a struct as below,

    typedef struct
    {
    uint16_t id;
    long offset;
    } foo_t;

    foo_t foo;

    somewhere in the code, i need the address of the 'id' member, so i get
    it using "& foo.id". this has no problem with GNU C compiler, but with
    another compile (IAR C), i got an warning,

    "warning use of address of unaligned structure member"

    so i want to ask, why using address of an unaligned structure memeber
    was considered as a fault. and how do i elimited it?

    thanks.

    -
    woody
     
    Steven Woody, Feb 8, 2006
    #1
    1. Advertisements

  2. Steven Woody

    Ian Collins Guest

    How is it unaligned? It's the first member of the the struct.

    A couple of questions spring to mind, what is the int size of your
    target and are you using any packing pragmas?
     
    Ian Collins, Feb 8, 2006
    #2
    1. Advertisements

  3. Steven Woody

    Steven Woody Guest

    sizeof(int) = 2 in the target. and i did not pack the structure.
     
    Steven Woody, Feb 8, 2006
    #3
  4. Steven Woody

    CBFalconer Guest

    As far as standards compliance is concerned, a compiler can warn
    about anything it wishes, including the atrocious color of your
    tie. But here it appears that the IAR compiler is just plain
    confused.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
    Also see <http://www.safalra.com/special/googlegroupsreply/>
     
    CBFalconer, Feb 8, 2006
    #4
  5. Steven Woody

    Ian Collins Guest

    It does, doesn't it? What does it say if you take the address of offset?
     
    Ian Collins, Feb 8, 2006
    #5
  6. Show us a complete compilable program that illustrates the problem.
    Be sure to include either the definition of uint16_t, or a #include
    for the header that defines it (<stdint.h> if you're using a C99
    implementation or a C90 implementation that provides it as an
    extension). Better yet, change the declaration of id to unsigned int
    (you said int is 16 bits on your implementation).

    For example, this program:

    #include <stdio.h>
    #include <stddef.h>

    int main(void)
    {
    typedef struct {
    unsigned int id;
    long offset;
    } foo_t;

    foo_t foo;

    unsigned int *ptr = &foo.id;

    printf("sizeof(unsigned int) = %d\n", (int)sizeof(unsigned int));
    printf("sizeof(long) = %d\n", (int)sizeof(long));
    printf("sizeof(foo_t) = %d\n", (int)sizeof(foo_t));
    printf("offsetof(foo_t, id) = %d\n", (int)offsetof(foo_t, id));
    printf("offsetof(foo_t, offset) = %d\n", (int)offsetof(foo_t, offset));

    return 0;
    }

    *might* trigger the warning. If it does, show us the exact diagnostic
    from the compiler and the output of the program. You can ignore any
    warning about ptr being unused. (If offsetof(foo_t, id) is anything
    other than 0, your implementation is badly broken.)
     
    Keith Thompson, Feb 8, 2006
    #6
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.