Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #77194

Re: Aliasing through a union pointer

From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c
Subject Re: Aliasing through a union pointer
Date 2015-11-26 12:58 +1300
Message-ID <dbn08lFea62U1@mid.individual.net> (permalink)
References (10 earlier) <10e6da2d-3050-4672-a66d-38fd9bddc22e@googlegroups.com> <dbkv7bFjpqnU26@mid.individual.net> <09b9443c-9033-4cb3-97e2-0e71d37e8cd5@googlegroups.com> <dbmi9gFea61U1@mid.individual.net> <286a81d0-5dfc-4ce5-ae56-060a7818c167@googlegroups.com>

Show all headers | View raw


supercat@casperkitty.com wrote:
> On Wednesday, November 25, 2015 at 1:59:58 PM UTC-6, Ian Collins wrote:
>> I'm sure any compiler could do that, but it would have to do so for any
>> assignment through a pointer to a a type other than (unsigned) char
>> unless it could prove the address was correctly aligned.  An alternative
>> is to trap on an access violation and perform the access byte by byte
>> (as on SPARC).  Neither should happen without the programmer making the
>> choice (as they do on SPARC).
>
> Perhaps you misunderstood my intention.  The idea was not to say that all
> pointers to integer types should tolerate unaligned loads, but rather that
> there should be a means of defining types with precise layout and alignment
> specifications, and that accesses *made through pointers to such types*
> should do whatever is necessary to accommodate them.

I see where you are coming from, but I don't think your suggestion goes 
far enough.  Consider passing the address of second to f() in this snippet:

packed struct S
{
   uint8_t  first;
   uint32_t second;
};

void f( uint32_t* );

<snip>

> In cases where a small embedded processor and a PC will exchange data in the
> native format of the embedded processor, the embedded processor may not be
> able to afford the code or CPU time necessary to manipulate everything using
> character types, but the PC certainly would in most cases easily be able to
> afford both.  I would suggest that even code designed around the embedded
> systems' storage formats would be much less efficient on the PC than would
> code that was designed around the PC's storage formats, there would still be
> great value in being able to have the PC "leisurely stroll" the same code
> that the embedded system could run.

There is - compiler extensions!  Supporting misaligned access impacts 
the entire program, not just specific accesses.

I have done a lot of work similar to what you describe above as well as 
running parts of the embedded code on a workstation host.  My solution 
on SPARC (which can't do packed structs) was to compile the embedded 
code as C++ and use customised types for the stricture members to handle 
the packing an unpacking.  A more general solution would be to use 
opaque types and accessor functions.

At the time, enabling misaligned access on the SPARC build cased the 
code to run slower than it did on the embedded target....

-- 
Ian Collins

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Aliasing through a union pointer Ian Pilcher <arequipeno@gmail.com> - 2015-11-23 10:14 -0600
  Re: Aliasing through a union pointer Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-23 11:43 -0500
    Re: Aliasing through a union pointer Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-23 11:49 -0500
    Re: Aliasing through a union pointer Ian Pilcher <arequipeno@gmail.com> - 2015-11-23 13:22 -0600
      Re: Aliasing through a union pointer Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-23 14:38 -0500
        Re: Aliasing through a union pointer Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-25 10:38 -0800
          Re: Aliasing through a union pointer Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-25 14:17 -0500
            Re: Aliasing through a union pointer Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-27 13:16 -0800
      Re: Aliasing through a union pointer David Brown <david.brown@hesbynett.no> - 2015-11-23 21:46 +0100
        Re: Aliasing through a union pointer Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-11-23 22:20 +0000
          Re: Aliasing through a union pointer Stephen Sprunk <stephen@sprunk.org> - 2015-11-23 16:52 -0600
            Re: Aliasing through a union pointer Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-11-23 23:10 +0000
              Re: Aliasing through a union pointer Stephen Sprunk <stephen@sprunk.org> - 2015-11-23 19:05 -0600
          Re: Aliasing through a union pointer Nick Bowler <nbowler@draconx.ca> - 2015-11-23 22:51 +0000
          Re: Aliasing through a union pointer Keith Thompson <kst-u@mib.org> - 2015-11-23 15:49 -0800
            Re: Aliasing through a union pointer supercat@casperkitty.com - 2015-11-23 16:01 -0800
          Re: Aliasing through a union pointer David Brown <david.brown@hesbynett.no> - 2015-11-24 09:55 +0100
            Re: Aliasing through a union pointer Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-11-24 14:29 +0000
              Re: Aliasing through a union pointer David Brown <david.brown@hesbynett.no> - 2015-11-24 22:58 +0100
                Re: Aliasing through a union pointer Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-24 17:30 -0500
                Re: Aliasing through a union pointer supercat@casperkitty.com - 2015-11-24 15:27 -0800
                Re: Aliasing through a union pointer Ian Collins <ian-news@hotmail.com> - 2015-11-25 12:57 +1300
                Re: Aliasing through a union pointer supercat@casperkitty.com - 2015-11-24 16:31 -0800
                Re: Aliasing through a union pointer Ian Collins <ian-news@hotmail.com> - 2015-11-25 18:28 +1300
                Re: Aliasing through a union pointer supercat@casperkitty.com - 2015-11-25 06:54 -0800
                Re: Aliasing through a union pointer Ian Collins <ian-news@hotmail.com> - 2015-11-26 08:59 +1300
                Re: Aliasing through a union pointer supercat@casperkitty.com - 2015-11-25 13:01 -0800
                Re: Aliasing through a union pointer Ian Collins <ian-news@hotmail.com> - 2015-11-26 12:58 +1300
                Re: Aliasing through a union pointer supercat@casperkitty.com - 2015-11-25 16:44 -0800
                Re: Aliasing through a union pointer Ian Collins <ian-news@hotmail.com> - 2015-11-26 14:12 +1300
                Re: Aliasing through a union pointer supercat@casperkitty.com - 2015-11-25 21:34 -0800
                Re: Aliasing through a union pointer Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-25 21:55 -0800
                Re: Aliasing through a union pointer Ian Collins <ian-news@hotmail.com> - 2015-11-26 20:51 +1300
                Re: Aliasing through a union pointer supercat@casperkitty.com - 2015-11-26 11:39 -0800
                Re: Aliasing through a union pointer Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-11-26 00:06 +0000
            Re: Aliasing through a union pointer Keith Thompson <kst-u@mib.org> - 2015-11-24 11:51 -0800
              Re: Aliasing through a union pointer supercat@casperkitty.com - 2015-11-24 12:20 -0800
                Re: Aliasing through a union pointer Keith Thompson <kst-u@mib.org> - 2015-11-24 21:52 -0800
                Re: Aliasing through a union pointer supercat@casperkitty.com - 2015-11-25 08:30 -0800
              Re: Aliasing through a union pointer David Brown <david.brown@hesbynett.no> - 2015-11-24 23:04 +0100
  Re: Aliasing through a union pointer Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-25 10:10 -0800
    Re: Aliasing through a union pointer Richard Damon <Richard@Damon-Family.org> - 2015-11-25 11:02 -0800
      Re: Aliasing through a union pointer Philip Lantz <prl@canterey.us> - 2015-11-26 13:06 -0800
        Re: Aliasing through a union pointer Richard Damon <Richard@Damon-Family.org> - 2015-11-26 13:09 -0800
      Re: Aliasing through a union pointer Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-27 13:08 -0800

csiph-web