niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » C
Accessing structure elements using pointer math - padding worries?
Post new topic   Reply to topic Page 1 of 1 [13 Posts] View previous topic :: View next topic
Author Message
J. J. Farrell
*nix forums addict


Joined: 09 Mar 2005
Posts: 57

PostPosted: Fri Jul 21, 2006 2:52 am    Post subject: Re: Accessing structure elements using pointer math - padding worries? Reply with quote

quantumred wrote:
Quote:
Thanks for all the helpful and detailed replies. I spent some time with
offsetof and sizeof and with my MSVC compiler (I am compiling as C, not
C++) it seems char padding is always byte aligned to 1 if the structure
contains only chars.

This is true even if I try to bump it up with #pragma pack(16)

You can always experiment to discover precisely what this one compiler
does in precisely the mode you're invoking it. But why bother? Do you
want to have to go through that every time someone upgrades the
compiler? Or changes the flags they pass to the comiler? Or uses a
different compiler? Or targets a different system?

Why not just write it in correct C so you're guaranteed it will work
with any options and any C compiler on any system?
Back to top
slebetman@gmail.com
*nix forums Guru Wannabe


Joined: 05 Nov 2005
Posts: 159

PostPosted: Fri Jul 21, 2006 2:22 am    Post subject: Re: Accessing structure elements using pointer math - padding worries? Reply with quote

quantumred wrote:
Quote:
Thanks for all the helpful and detailed replies. I spent some time with
offsetof and sizeof and with my MSVC compiler (I am compiling as C, not
C++) it seems char padding is always byte aligned to 1 if the structure
contains only chars.

This is true even if I try to bump it up with #pragma pack(16)

Try compiling it for WinCE target on an ARM target. Memory on the x86
platform are byte aligned. But not all CPUs are byte aligned and not
all popular OSes run on x86. Now with .Net being mainly a bytecode
interpreter Microsoft may in the future be free to re-align their
strategy and use a different family of processors (like uh.. I don't
know, PowerPC maybe? like on the new Xbox?).

Linux already run on a variety of CPUs where sloppy code have caused
byte-alignment and endianness problems. Today almost all of the most
popular open source software have been cleaned up thanks in no small
part to people who were determined to port them to those exotic (and
some, like ARM, common) CPUs. Don't repeat the mistakes of those who've
gone through this before.

The lesson is to stick to the C standard and listed to the advice of
seasoned C gurus. Don't believe everything your compiler says is OK.
Remember that the C standard was written in part to make it easy to
implement optimising compilers. Compilers may seem to implement such
extra "features" like byte-wise alignment of structs but they are in
fact just a characteristic of your underlying machine - not really a
feature of C itself.
Back to top
Frederick Gotham
*nix forums Guru


Joined: 09 Jun 2006
Posts: 502

PostPosted: Thu Jul 20, 2006 1:39 pm    Post subject: Re: Accessing structure elements using pointer math - padding worries? Reply with quote

Quote:
c++; // this is okay?


No. The implementation is free to insert padding between struct members.

There was a discussion about this over on comp.std.c++. It mostly talks about
C features though, so you should be able to follow the discussion:

http://groups.google.ie/group/comp.std.c++/browse_frm/thread/fbca48fa4595ff9/
fbf173cad0ec4574?lnk=st&q=&rnum=1&hl=en#fbf173cad0ec4574


--

Frederick Gotham
Back to top
quantumred@gmail.com
*nix forums beginner


Joined: 19 Jul 2006
Posts: 2

PostPosted: Thu Jul 20, 2006 3:48 am    Post subject: Re: Accessing structure elements using pointer math - padding worries? Reply with quote

Thanks for all the helpful and detailed replies. I spent some time with
offsetof and sizeof and with my MSVC compiler (I am compiling as C, not
C++) it seems char padding is always byte aligned to 1 if the structure
contains only chars.

This is true even if I try to bump it up with #pragma pack(16)

Quote:
If you desire to treat them like an array, why not make them an array
contained in the struct?

I realize I could just use an array. I'm not determined to implement
such code as described in the original post, but I like to be aware of
my options.
Back to top
Default User
*nix forums Guru


Joined: 21 Feb 2005
Posts: 1159

PostPosted: Wed Jul 19, 2006 4:53 pm    Post subject: Re: Accessing structure elements using pointer math - padding worries? Reply with quote

quantumred@gmail.com wrote:

Quote:
Can I use pointer arithmetic on the members of a structure in the
following way? Should I be worried about structure padding? This works
in my debugger but I wonder if I'm bending some rule here.

struct char_struct {
unsigned char a;
unsigned char b;
unsigned char c;
unsigned char d;
};

struct char_struct test = {50,75,100,225};

unsigned char *c;

c = &test;

if (c[2]==100) // this is okay?
printf("c[2] is 100"); // this does work


If you desire to treat them like an array, why not make them an array
contained in the struct?




Brian
Back to top
slebetman@gmail.com
*nix forums Guru Wannabe


Joined: 05 Nov 2005
Posts: 159

PostPosted: Wed Jul 19, 2006 9:18 am    Post subject: Re: Accessing structure elements using pointer math - padding worries? Reply with quote

Richard Heathfield wrote:
Quote:
spibou@gmail.com said:

EventHelix.com wrote:

This may not work on a platform where
the char's get padded to 16 or 32 bit boundaries.

Are there any such platforms ?

Yes. For example, consider platforms where CHAR_BIT is 16 or 32.

(And yes, there are such platforms. Digital signal processors typically have
gurt big chars.)

DSP are a bit too exotic for some people to consider significant.
They'll say "but, but it works on my PC!!". I always like to point out
that most ARM compilers do pad chars to 32bit boundries. If they still
look confused and don't get it I'll politely point out that XScale is
ARM. If they still don't get it I'll casually mention that XScale is
what most PocketPC run on and that almost 90% of all Windows CE devices
(not to mention Palm and Psion/Symbian) run on ARM.
Back to top
neutron*star
*nix forums Guru


Joined: 21 Feb 2005
Posts: 2039

PostPosted: Wed Jul 19, 2006 7:23 am    Post subject: Re: Accessing structure elements using pointer math - padding worries? Reply with quote

spibou@gmail.com said:

Quote:
EventHelix.com wrote:

This may not work on a platform where
the char's get padded to 16 or 32 bit boundaries.

Are there any such platforms ?

Yes. For example, consider platforms where CHAR_BIT is 16 or 32.

(And yes, there are such platforms. Digital signal processors typically have
gurt big chars.)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Back to top
spibou@gmail.com
*nix forums Guru Wannabe


Joined: 29 May 2006
Posts: 122

PostPosted: Wed Jul 19, 2006 6:19 am    Post subject: Re: Accessing structure elements using pointer math - padding worries? Reply with quote

EventHelix.com wrote:

Quote:
This may not work on a platform where
the char's get padded to 16 or 32 bit boundaries.

Are there any such platforms ?
Back to top
Barry Schwarz
*nix forums Guru


Joined: 09 Apr 2005
Posts: 390

PostPosted: Wed Jul 19, 2006 3:59 am    Post subject: Re: Accessing structure elements using pointer math - padding worries? Reply with quote

On 18 Jul 2006 20:11:09 -0700, quantumred@gmail.com wrote:

Quote:
Can I use pointer arithmetic on the members of a structure in the
following way? Should I be worried about structure padding? This works

Yes you should.

Quote:
in my debugger but I wonder if I'm bending some rule here.

Murphy's Law. If something can go wrong it will, but only at the
worst possible time. Like when your most important customer tries to
compile the code on his system.

Quote:

struct char_struct {
unsigned char a;
unsigned char b;
unsigned char c;
unsigned char d;
};

struct char_struct test = {50,75,100,225};

unsigned char *c;

c = &test;

if (c[2]==100) // this is okay?
printf("c[2] is 100"); // this does work

c++; // this is okay?
printf("*c is 75"); // this does work


Remove del for email
Back to top
Gordon Burditt
*nix forums Guru


Joined: 02 Mar 2005
Posts: 773

PostPosted: Wed Jul 19, 2006 3:36 am    Post subject: Re: Accessing structure elements using pointer math - padding worries? Reply with quote

Quote:
Can I use pointer arithmetic on the members of a structure in the
following way?

No.

Quote:
Should I be worried about structure padding?

Yes.

Quote:
This works
in my debugger but I wonder if I'm bending some rule here.

Is blowing to smithereens with C4 considered "bending"?

Quote:
struct char_struct {
unsigned char a;
unsigned char b;
unsigned char c;
unsigned char d;
};

If you want an array as a structure member, USE an array as a
structure member.

Quote:
struct char_struct test = {50,75,100,225};

unsigned char *c;

c = &test;

if (c[2]==100) // this is okay?
No.
printf("c[2] is 100"); // this does work

On YOUR version of YOUR compiler and perhaps only as long as you
don't add a double as a fifth member of the structure.

Quote:

c++; // this is okay?

Yes. Consider a single variable as an array of one element. You
are allowed to compute the address of "one past the last element"
but not dereference it.

Quote:
printf("*c is 75"); // this does work

Ok, printing a constant string is allowed, but if you were planning
on dereferencing c after incrementing it, NO, that's not allowed.

Gordon L. Burditt
Back to top
EventHelix.com
*nix forums addict


Joined: 23 Feb 2005
Posts: 92

PostPosted: Wed Jul 19, 2006 3:34 am    Post subject: Re: Accessing structure elements using pointer math - padding worries? Reply with quote

Quote:
Can I use pointer arithmetic on the members of a structure in the
following way? Should I be worried about structure padding? This works
in my debugger but I wonder if I'm bending some rule here.

struct char_struct {
unsigned char a;
unsigned char b;
unsigned char c;
unsigned char d;
};

struct char_struct test = {50,75,100,225};

unsigned char *c;

c = &test;

if (c[2]==100) // this is okay?
printf("c[2] is 100"); // this does work

c++; // this is okay?
printf("*c is 75"); // this does work

This type of access is not recommended. In compiler is not adding any
pads so your test program works. This may not work on a platform where
the char's get padded to 16 or 32 bit boundaries.

The following article should help:
http://www.eventhelix.com/RealtimeMantra/ByteAlignmentAndOrdering.htm

--
EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
Sequence Diagram Based Real-time and Embedded System Design Tool
Back to top
J. J. Farrell
*nix forums addict


Joined: 09 Mar 2005
Posts: 57

PostPosted: Wed Jul 19, 2006 3:28 am    Post subject: Re: Accessing structure elements using pointer math - padding worries? Reply with quote

quantumred@gmail.com wrote:
Quote:
Can I use pointer arithmetic on the members of a structure in the
following way?

Yes, but not quite as you mean.

Quote:
Should I be worried about structure padding?

Yes.

Quote:
This works
in my debugger but I wonder if I'm bending some rule here.

struct char_struct {
unsigned char a;
unsigned char b;
unsigned char c;
unsigned char d;
};

struct char_struct test = {50,75,100,225};

unsigned char *c;

c = &test;

if (c[2]==100) // this is okay?

It's OK in the sense that "you can address anything through an unsigned
character pointer", and the index is less that the size of the
structure. It doesn't necessarily pick up member c of the structure
though.

Quote:
printf("c[2] is 100"); // this does work

By coincidence, depending on the compiler/platform.

Quote:
c++; // this is okay?

Yes, as long as you stay within the structure or step to just beyond
it.

Quote:
printf("*c is 75"); // this does work

Again, not guaranteed to be member d.
Back to top
quantumred@gmail.com
*nix forums beginner


Joined: 19 Jul 2006
Posts: 2

PostPosted: Wed Jul 19, 2006 3:11 am    Post subject: Accessing structure elements using pointer math - padding worries? Reply with quote

Can I use pointer arithmetic on the members of a structure in the
following way? Should I be worried about structure padding? This works
in my debugger but I wonder if I'm bending some rule here.

struct char_struct {
unsigned char a;
unsigned char b;
unsigned char c;
unsigned char d;
};

struct char_struct test = {50,75,100,225};

unsigned char *c;

c = &test;

if (c[2]==100) // this is okay?
printf("c[2] is 100"); // this does work

c++; // this is okay?
printf("*c is 75"); // this does work
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [13 Posts] View previous topic :: View next topic
The time now is Fri Nov 21, 2008 7:02 pm | All times are GMT
navigation Forum index » Programming » C
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Problem while accessing 64 bit Server thru 32 bit client Ganesh Server 0 Fri Jul 21, 2006 1:40 pm
No new posts Function Pointer Sikandar C 3 Fri Jul 21, 2006 1:23 pm
No new posts determine pointer to point to array or single item during... yancheng.cheok@gmail.com C++ 5 Fri Jul 21, 2006 1:17 am
No new posts FAQ 4.41 How can I remove duplicate elements from a list ... PerlFAQ Server Perl 0 Fri Jul 21, 2006 1:03 am
No new posts Datablock structure: changes from a version to another ? Spendius Server 2 Thu Jul 20, 2006 10:26 am

Free Ringtones | Secured Loans | Debt Consolidation | Loans | Cell Phones
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.3235s ][ Queries: 20 (0.1955s) ][ GZIP on - Debug on ]