Yahoo Groups archive

Lpc2000

Index last updated: 2026-04-28 23:31 UTC

Message

Re: For C Experts

2006-03-30 by dsidlauskas1

ints are not always word aligned. For example:

#pragma packed(1)
struct eg
     {
     char x;
     int y;
     char z;
     int w;
     }test;

One of the integers in this structure is not word word aligned and yet
reference to the non word aligned integer works fine, as should

int x, *ip;
ip = &test.w;
x = *ip;


--- In lpc2000@yahoogroups.com, "fordp2002" <SimonEllwood@...> wrote:
>
> By casting buf to an int you have instructed the compiler NOT to treat
> it as a byte.
> 
> It is beholden on you the software engineer to ensure whatever you
> cast is compatible with what you cast it too. In this case you are
> saying that &buf[i] is word aligned, which will be only true for one
> in four cases of i. If the compiler aligns the start of buf to a word
> bondary which will happen under some cases &buf[0] will be the only
> legal cast.
> 
> FordP
> 
> 
> --- In lpc2000@...m, David Hawkins <dwh@> wrote:
> >
> > dsidlauskas1 wrote:
> > > Consider the following code:
> > > 
> > > ============================
> > > char buf[]={1,2,3,4,5,6,7,8};
> > > int *ip, x[4];
> > > 
> > > for (i=0; i<4; i++)
> > >     {
> > >     ip = (int *)&buf[i];
> > >     x[4]  = *ip;
> > >     }
> > 
> > Er, given the fact that x is of length 4,
> > the statement x[4] = *ip is actually out-of-bounds.
> > 
> > Dave
> >
>

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.