473,785 Members | 2,388 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13924
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.c om, 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/googlegroupsrep ly/>
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********@gma il.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(uns igned int));
printf("sizeof( long) = %d\n", (int)sizeof(lon g));
printf("sizeof( foo_t) = %d\n", (int)sizeof(foo _t));
printf("offseto f(foo_t, id) = %d\n", (int)offsetof(f oo_t, id));
printf("offseto f(foo_t, offset) = %d\n", (int)offsetof(f oo_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_Keit h) 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
9403
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 follows (1 BYTE) then actual data
6
2082
by: archilleswaterland | last post by:
structures typedef struct{ char name; int age; float balance; }account; account xyx; accout *ptr;
18
10077
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: long_pointer = (some unaligned address) long_value = (* (__unaligned long *) long_pointer );
30
6099
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 typedefed as typedef long off_t; I wanted to know what advantage do we get by typedefs ? Why we did not declare
13
5024
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
5446
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 *)(ptr)-(unsigned long)(&((type *)0)->member))) Suppose you have struct foobar and want to compute the offset of element boo, this is how it is done: (unsigned long)(&((struct foobar *)0)->boo)
10
7493
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
6024
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 foo *next; int a; int b; int c; };
14
28167
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
9491
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10357
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10104
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4063
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.