Re: alias question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> That's not C code.  I have no idea about
> the rules in C++..

Apology, I'll try to stick to C code.

> > What?  You can access any type with a char type,
> > this is a specific exception.
> 
> Yes.  And you cannot access a char as any type. 
> The exception is not symmetric in that way.

I looked this up.  That's just legalese wording to stop the
following "logic" of attempting to alias through char:

uint32_t *d;
char     *c =     (char*)d;  /* okay */
uint16_t *s = (uint16_t*)c;  /* bzzt spec says no! */

Clearly the above code will fail when written by *s and read
by *d, or vice-versa.  Not because we created an alias to
char, but because we sneakily created an alias between two
different non-char types through the char pointer.

If there is only char + one type in question, then the reality is
that it make no difference which order the aliases are created.
There are two pointers that alias each other.  One can't say that
the char aliases the type, or that the type aliases the char,
it's equivalent at that point.

My example is different that any other example that I could dig
up on the web.  I've concluded that the reason my example code is
valid and works is because there are only char writes.  Any other
type of read to the same region will always see the char writes,
even if those other reads happen to overlap.

Counter-example code that breaks always welcome!

Thanks,
Jason







[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux