473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

using member address of an unaligned structure

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
5 13799
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

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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Panchal V | last post by:
I want to access a variable length record in C, the format is as follows : +---+---+-----------+ | A | L | D A T A | +---+---+-----------+ A - Some Data (1 BYTE) L - Length the Data that...
6
by: archilleswaterland | last post by:
structures typedef struct{ char name; int age; float balance; }account; account xyx; accout *ptr;
18
by: Pavel A. | last post by:
Hello, What is equivalent of __unaligned attribute in GNU C? I've searched for some time and it seems that it just does not exist... /* For HP and new Microsoft compilers it works this way:...
30
by: junky_fellow | last post by:
I was looking at the source code of linux or open BSD. What I found that lots of typedefs were used. For example, consider the offset in a file. It was declared as off_t offset; and off_t is...
13
by: Kantha | last post by:
Hi all, I have declared an Union as follows typedef union { struct interrupt_bits { unsigned char c_int_hs_fs_status : 1, c_setup_intflag : 1,
9
by: cman | last post by:
What are the mechanics involved in the calculation of an offset of a structure member, demonstrated in this piece of code? #define list_entry(ptr, type, member) \ ((type *)((char...
10
by: =?Utf-8?B?TmFuZCBLaXNob3JlIEd1cHRh?= | last post by:
I have a binary file created using C++ that contains an object of the structure: struct student { int roll_no; char name; char qualification; };
6
by: Urs Thuermann | last post by:
With offsetof() I can get the offset of a member in a struct. AFAICS, it is portable and clean to use this offset to access that member. I need to do something like this struct foo { struct...
14
by: deepak | last post by:
Hi Experts, I'm getting this compilation error while trying to access a member in structure. at what time we will get this error message? Thanks, Deepak
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.