Yahoo Groups archive

Lpc2000

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

Message

Re: For C Experts

2006-03-31 by brendanmurphy37

This is also non-portable.

Brendan

--- In lpc2000@yahoogroups.com, Sten <list@...> wrote:
>
> Hello,
> 
> Another solution I had last night which should fix the alignment-
problem, because the union is
> compiler will align this union to its biggest member:
> ===============================================================
> union {
>         char    u8[8];
>         short   u16[4];
> } buff = {1, 2, 3, 4, 5, 6, 7, 8};
> 
> int main(void) {
>         short x[10], i;
> 
>         for (i = 0; i < 4; i++) {
>                 x[i] = buff.u16[i];
>                 printf(" %04x, ", x[i]);
>         }
> 
>         return 0;
> }
> ===============================================================
> 
>    Sten
> 
> brendanmurphy37 wrote:
> > Dave,
> > 
> > Think about whet your saying here:
> > 
> > 
> >>ip = (int *)&buf[5];
> >>
> >>should be treated by the compiler as:
> >>
> >>ip = (__packed int *)&buf[5];
> >>
> > 
> > 
> > "should" doesn't come into it! You have explicitley told the 
> > compiler exectly how you want it treated (the "(int *)"). Now if 
you 
> > had one of those magic "do what I mean, not what I say" 
compilers, 
> > you'd be fine......
> > 
> > By the way, if you find that compiler, let me know!
> > 
> > Brendan
> > 
> > --- In lpc2000@yahoogroups.com, "dsidlauskas1" <dsidlauskas@> 
> > wrote:
> > 
> >>Jaya,
> >>
> >>I don't think I'm asking it to connect the two statements.
> >>
> >>What I'm expecting it to do is to recognize that buf is a byte 
> > 
> > packed
> > 
> >>object (the defalt for the compler)and when casting byte packed
> >>objects the compiler should cast them to other byte packed 
objects.
> >>
> >>in otherwords;
> >>
> >>char buf[10];
> >>int ip*;
> >>
> >>ip = (int *)&buf[5];
> >>
> >>should be treated by the compiler as:
> >>
> >>ip = (__packed int *)&buf[5];
> >>
> >>and this is because char buf[10] is a byte packed object for this
> >>compiler.
> >>
> >>Dave
> >>
> >>
> >>--- In lpc2000@yahoogroups.com, "jayasooriah" <jayasooriah@> 
wrote:
> >>
> >>>Dave,
> >>>
> >>>I think what you are asking of the compiler is to connect the 
two
> >>>statements in the for() loop.  Compilers do not have this 
> > 
> > capability.
> > 
> >>> Even if it did, it would have to also consider the for() 
> > 
> > statement
> > 
> >>>too, as your code would be legitimate if the for() statement 
was:
> >>>
> >>>
> >>>> for (i = 0; i < ...; i += 4)
> >>>
> >>>A purist would argue is that there is nothing wrong with the 
> > 
> > pointer
> > 
> >>>assignment given the explicit cast.  The error occurs only if 
> > 
> > (and
> > 
> >>>when) the pointer is dereferenced at runtime.
> >>>
> >>>Making it a packed array on these grounds may not be what the
> >>>programmer intended.  Besides, packed array methods involve
> >>>implementation issues.
> >>>
> >>>GCC was kind enough to allude you to the possibility of such an 
> > 
> > event
> > 
> >>>ocurring.  Note that it too did not make the relationship 
> > 
> > between the
> > 
> >>>two statements.  If it did, it would report an error, not a 
> > 
> > warning.
> > 
> >>>Jaya
> >>>
> >>>--- In lpc2000@yahoogroups.com, "dsidlauskas1" <dsidlauskas@> 
> > 
> > wrote:
> > 
> >>>>Jaya,
> >>>>
> >>>>Since the compiler knows that buf is a byte aligned object, 
> > 
> > woutldn't
> > 
> >>>>it be resonable for the compiler to treat
> >>>>
> >>>>(int *)&buf[i]
> >>>>
> >>>>as 
> >>>>
> >>>>(__packed int *)&buf[i]
> >>>>
> >>>>In general, if non-naturaly aligned objects are promoted by a 
> > 
> > cast,
> > 
> >>>>shouldn't, by default, the cast be of the proper non-aligned 
> > 
> > type?
> > 
> >>>>BTW. Really appreciate your discussion of interupts on your 
> > 
> > web site.
> > 
> >>>>Dave
> >>>
> > 
> > 
> > 
> > 
> > 
> > 
> >  
> > Yahoo! Groups Links
> > 
> > 
> > 
> >  
> > 
> > 
> > 
> 
> 
> -- 
> /************************************************
>  Do you need a tiny and efficient real time
>  operating system (RTOS) with a preemtive
>  multitasking for LPC2000 or AT91SAM7?
> 
>    http://nanortos.net-attack.de/
> 
>  Or some open-source tools and code for LPC2000?
> 
>    http://www.net-attack.de/
> 
> ************************************************/
>

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.