> Bryan Parkoff wrote:
>>> Bryan Parkoff wrote:
>>>> [..] I feel nameless union/struct is necessary. I want three
>>>> variables to share one big variable. You want to work
>>>> two byte data. It causes word data to be modified automatically
>>>> because it is shared.
>>>
>>> That is your mistake. The language explicitly states that you only
>>> can read the same data you wrote. You cannot write byte and then
>>> read word. That's not what the unions are for. To accomplish that
>>> you need to 'static_cast' your word into an array of char and then
>>> change each char as you want.
>>
>> Please explain why you think that union is not to be used.
>
> I don't have to. The language Standard forbids it. If I had to
> speculate it's because you either need to explicitly allow certain
> combinations (thus making a relatively long set of pairs that are
> OK to share the memory and let you read *not* what you wrote), or
> you disallow everything (like the Standard does) because there are
> combinations (like chars and a pointer, for instance) which are by
> *no* means OK. You cannot write a bunch of chars and then expect
> them to form a valid pointer, and even _reading_ (loading into
> an address register) an invalid pointer can cause hardware fault
> on some systems.
OK, I understand. It looks like non-standard C++ Compiler to accept
four byte variables to be linked into one dword variable using union. I
always decide to allow overcoming non-standard C++ Compiler. I hope that it
should be compatible to all C++ Coompiler like Microsoft, GNU, Mac OSX, and
others.
static_cast<> is used only if I want to convert small size to big size,
but not shared / linked small / big sizes. Hopefully, C++ Compiler should
be able to implement to support non-standard C++ near the future so this
code can be very good portablility.
Thank you for your comment. Smile...
Bryan Parkoff
|