It's not at all clear what the actual intent of the programmer is for
this: what's actually coded is a long way from memcpy, regardless of
any assumptions you make about what the compiler does.
I'm actually a bit surprised that GCC gives a warning: the only
really incorrect line (language-wise) is the "ip = (int *)&buf[i];"
one, but the programmer here gives an explicit direction to the
compiler (by using the explicit type cast) that they know what
they're doing. Most compilers would I suspect not issue a warning in
that circumstance.
All of which begs the question: what is the original intent?
Brendan
--- In lpc2000@yahoogroups.com, Robert Adsett <subscriptions@...>
wrote:
>
> Quoting dsidlauskas1 <dsidlauskas@...>:
>
> > 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;
> > }
> > =============================
> > The Keil compiler compiles this without warning, but does not
produce
> > the expected (for me) result in x. The problem is that Keil uses
the
> > LDR instruction to effect the transfer and this is valid only on 4
> > byte boundaries.
> >
> > GCC compiles but does give a non-aligned access warning.
> >
> > I believe that the compiler has enough information to use byte
aligned
> > transfers, and should, or at least give a warning.
> >
> > Anybody want to weigh in on this one.
>
> The short answer is that the code is broken. The only semi-
portable way to do
> this is memcpy.
>
> There is no requirement to provide a warning in this case AFAIK.
That's a
> quality of implementation issue.
>
> Robert
>Message
Re: For C Experts
2006-03-30 by brendanmurphy37
Attachments
- No local attachments were found for this message.