Article View: comp.std.c
Article #4217Re: (no subject) [cast expression as an lva
From: Steve Clamage
Date: Wed, 11 Oct 1995 18:22
Date: Wed, 11 Oct 1995 18:22
50 lines
2847 bytes
2847 bytes
In article 95Oct11105909@slsvgqt.lts.sel.alcatel.de, kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763) writes: >In article <45fgea$d8j@yage.tembel.org> shields@tembel.org (Michael >Shields) writes: > >|> In article <45ef3f$n8u@engnews2.Eng.Sun.COM>, >|> Steve Clamage <clamage@Eng.Sun.COM> wrote: >|> > What's interesting is that you can get the same effect with valid code >|> > that should generate the same object code as the invalid original. >|> > For example, instead of >|> > ((char*)src)++; >|> > you can write >|> > (*(char**)(&src))++; >|> > I don't claim this code is more readable, but you could encapsulate the >|> > ugliness in a macro. For example: >|> > #define increment_as(ptr, T) ((*(T*)(&ptr))++) >|> > increment_as(src, char*); > >|> If it is always possible, why not just legalize the first syntax? > >Because it fails miserably on machines where different types of >pointers have different representations. I believe it was Henry >Spencer who said something about lying to the compiler... > >The problem I think Steve Clamage was addressing is that there are an >awful lot of programmers out there who think that the whole world is >composed of Vaxes and Suns. Their programs once worked on one of >these machines, and they expect everyone else to perpertuate their >errors. I failed to say that I was not advocating this style of programming, nor claiming the the code was always valid. As a number of people have pointed out, you cannot assume the general case is ever valid. What I wanted to say was in those cases where ((char*)src)++ did what was intended -- and as Chris Torek pointed out, what was intended was src = (int *)((char *)src + 1); you can write the code in a way that doesn't violate C rules and that doesn't need to generate any additional code. For that matter, Chris's rewrite need not generate any additional code either. I chose a rewrite in the same style as the original to forgo complaints about having to write 'src' twice, or the like. The specific case of incrementing an int* by one byte will of course have bad results on many architectures. Similar code may have a place in solving particular problems on particular architectures. A va_arg macro might wind up with similar-looking code, for example; the code would be tailored to each compiler on each platform. --- Steve Clamage, stephen.clamage@eng.sun.com
Technical Details
Message-ID:
<45h21s$84h@engnews2.Eng.Sun.COM>Path:
arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!usenetReferences:
<KANZE.95Oct11105909@slsvgqt.lts.sel.alcatel.de>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!usenet From: clamage@Eng.Sun.COM (Steve Clamage) Newsgroups: comp.std.c Subject: Re: (no subject) [cast expression as an lva Date: 11 Oct 1995 18:22:52 GMT Organization: Sun Microsystems Inc. Message-ID: <45h21s$84h@engnews2.Eng.Sun.COM> References: <KANZE.95Oct11105909@slsvgqt.lts.sel.alcatel.de> Reply-To: clamage@Eng.Sun.COM NNTP-Posting-Host: taumet.eng.sun.com