using member address of an unaligned structure

S

Steven Woody

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
 
I

Ian Collins

Steven said:
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?
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?
 
S

Steven Woody

Ian said:
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?

sizeof(int) = 2 in the target. and i did not pack the structure.
 
C

CBFalconer

Steven said:
sizeof(int) = 2 in the target. and i did not pack the structure.

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/>
 
I

Ian Collins

CBFalconer said:
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.
It does, doesn't it? What does it say if you take the address of offset?
 
K

Keith Thompson

Steven Woody said:
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?

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.)
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top