Yahoo Groups archive

Lpc2000

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

Message

Re: For C Experts

2006-03-30 by dsidlauskas1

OK. To the intent.

I've got a lot of code written for an 8 bit Z80 controller to port.
It does a lot of parsing of data out of communications buffers, thus
giving rise to the constructs of the type shown, i.e., figure out
where the object that you want is in the buffer, take that address and
cast it to a pointer of the proper type. The buffers always have byte
aligned data.

Now if you do something like;

#pragma pack(1)
struct my_buf {
      char buf[]={1,2,3,4,5,6,78};
      }b;

then if you replace buf with b.buf in the example it works fine.

Or many compilers have the keyword __packed. and if the interger
pointer is declared with:

__packed int *ip;

The example works fine. 

The problem when porting  is to find all of the places where this type
of declaration needs to be introduced. Often the problem will not show
itself because the buffer variable ends up being aligned. One slight
change to the memory map can bring about a problem totally
unrelated to the change.

I realize there's enough weasel room in the C99 spec to get the
compiler writers off the hook, but if their market is moving up from
the 8 to 32 biter's shouldn't they try harder?

Thanks for your comments.

Dave S.

--- In lpc2000@yahoogroups.com, Robert Adsett <subscriptions@...> wrote:
>
> Quoting brendanmurphy37 <brendan.murphy@...>:
> 
> >
> > 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.
> 
> Actually, I thought (still think) the intent is pretty obvious. 
Take whatever
> 'starts' at buf[i] and put it into an int.  That can be implemented
pretty
> straightforwardly by a memcpy.  What do you see that memcpy wouldn't do?
> 
> > 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.
> 
> I'm going to have to see what lint does when confronted with this. 
I expect
> it's going to be pretty verbose ;)
> 
> > All of which begs the question: what is the original intent?
> 
> Robert
>

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.