By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,680 Members | 1,753 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,680 IT Pros & Developers. It's quick & easy.

using member address of an unaligned structure

P: n/a
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

Feb 8 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Steven Woody wrote:
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?

--
Ian Collins.
Feb 8 '06 #2

P: n/a

Ian Collins wrote:
Steven Woody wrote:
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?


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

Feb 8 '06 #3

P: n/a
Steven Woody wrote:
Ian Collins wrote:
Steven Woody wrote:

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?


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/>
Feb 8 '06 #4

P: n/a
CBFalconer wrote:
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?


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.

It does, doesn't it? What does it say if you take the address of offset?

--
Ian Collins.
Feb 8 '06 #5

P: n/a
"Steven Woody" <na********@gmail.com> writes:
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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Feb 8 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.