Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > nameless struct / union

Reply
Thread Tools

nameless struct / union

 
 
Bryan Parkoff
Guest
Posts: n/a
 
      12-10-2007
I hate using struct / union with dot between two words. How can I use
one word instead of two words because I want the source code look reading
clear. three variables are shared inside one variable. I manipulate to
change 8-bit data before it causes to change 16-bit data and 32-bit data.
For example.

union

{

struct _Byte

{

U_BYTE AAL;

U_BYTE AAH;

} Byte;

struct _Word

{

U_WORD AAW;

} Word;

struct _DWORD

{

U_DWORD AA;

} DWord;

};

int main()

{

// I hate dot between 2 words.

Byte.AAL = 0xFF;

Byte.AAH = 0x20;

Byte.AAL += 0x0A;

Byte.AAH += 0x01;

Word.AAW += 0xFF;

DWord.AA += 0xFFFF;

// It is easy reading variable inside struct / union.

AAL = 0xFF;

AAH = 0x20;

AAL += 0x0A;

AAH += 0x01;

AAW += 0xFF;

AA += 0xFFFF;


--

Bryan Parkoff


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-10-2007
Bryan Parkoff wrote:
> I hate using struct / union with dot between two words. How can I
> use one word instead of two words because I want the source code look
> reading clear. three variables are shared inside one variable. I
> manipulate to change 8-bit data before it causes to change 16-bit
> data and 32-bit data. For example.
>
> union
>
> {
>
> struct _Byte
>
> {
>
> U_BYTE AAL;
>
> U_BYTE AAH;
>
> } Byte;
>
> struct _Word
>
> {
>
> U_WORD AAW;
>
> } Word;
>
> struct _DWORD
>
> {
>
> U_DWORD AA;
>
> } DWord;
>
> };
>
> int main()
>
> {
>
> // I hate dot between 2 words.
>
> Byte.AAL = 0xFF;
>
> Byte.AAH = 0x20;
>
> Byte.AAL += 0x0A;
>
> Byte.AAH += 0x01;
>
> Word.AAW += 0xFF;
>
> DWord.AA += 0xFFFF;
>
> // It is easy reading variable inside struct / union.
>
> AAL = 0xFF;
>
> AAH = 0x20;
>
> AAL += 0x0A;
>
> AAH += 0x01;
>
> AAW += 0xFF;
>
> AA += 0xFFFF;


Uh... I'm a bit rusty on unnamed unions. Does an unnamed union
create a global instance?

Also, your use of first assigning one part of the union and then
using another part has undefined behaviour, IIRC.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      12-10-2007
Victor Bazarov wrote:

> Uh... I'm a bit rusty on unnamed unions. Does an unnamed union
> create a global instance?


I didn't even know it's allowed outside a struct. Is it actually?

> Also, your use of first assigning one part of the union and then
> using another part has undefined behaviour, IIRC.


Yes. You must always only read the member you last wrote to, otherwise the
behavior is undefined.

 
Reply With Quote
 
Craig Scott
Guest
Posts: n/a
 
      12-10-2007
On Dec 11, 5:49 am, "Bryan Parkoff" <(E-Mail Removed)> wrote:
> I hate using struct / union with dot between two words. How can I use
> one word instead of two words because I want the source code look reading
> clear. three variables are shared inside one variable. I manipulate to
> change 8-bit data before it causes to change 16-bit data and 32-bit data.
> For example.
>
> union
> {
> struct _Byte
> {
> U_BYTE AAL;
> U_BYTE AAH;
> } Byte;
>
> struct _Word
> {
> U_WORD AAW;
> } Word;
>
> struct _DWORD
> {
> U_DWORD AA;
> } DWord;
>
> };


Sorry, it's not answering your original post (others seem to be doing
that already), but using a type name which starts with an underscore
and is followed by an uppercase letter is not allowed by the C++
standard (unless your code is part of the compiler implementation
itself). A name starting with an underscore and NOT followed by an
uppercase letter cannot be used in the global namespace, but
presumably could be elsewhere (but I'd recommend against it to avoid
confusion). See section 17.4.3.1.2 of the standard for details.

--
Computational Modeling, CSIRO (CMIS)
Melbourne, Australia
 
Reply With Quote
 
Bryan Parkoff
Guest
Posts: n/a
 
      12-10-2007
>> Uh... I'm a bit rusty on unnamed unions. Does an unnamed union
>> create a global instance?

>
> I didn't even know it's allowed outside a struct. Is it actually?
>
>> Also, your use of first assigning one part of the union and then
>> using another part has undefined behaviour, IIRC.

>
> Yes. You must always only read the member you last wrote to, otherwise the
> behavior is undefined.


I want to follow up. 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. For
example, you define Low_Byte and High_Byte. You add 0xFF + 0x03. Low_Byte
is modified to show 0x02. It does not modify High_Byte when one bit fell
off Low_Byte to become Carry. Then Carry can be added to High_Byte. It
makes easier so I do not have to use Word &= 0x00FF. If I want to work word
data, I do not need Carry and add can be 0x20FF + 0x0003. Word is modified
to show 0x2102.
It makes my source code readable clearly. Here is an exmaple below.
Please let me know what you think.

tatic union

{

U_BYTE B[4];

U_WORD W[2];

U_DWORD DW;

};

#define Low_Byte B[0]

#define High_Byte B[1]

#define Low_Byte2 B[2]

#define High_Byte2 B[3]

#define Low_Word W[0]

#define High_Word W[1]

#define DW DWord



int main()

{

Low_Byte = 0xFF;

High_Byte = 0x20;

Low_Byte += 0x03;

High_Byte += 0x01;

Low_Word += 0x00FF;

DWord += 0x0000FFFF;

return 0;

}


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-11-2007
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.

> [..]


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Bryan Parkoff
Guest
Posts: n/a
 
      12-11-2007
> 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 want to
share two bytes into one word. I can modify one low byte at this time and
high byte next time. Then word gets data from 2 bytes. You can define one
array with two elements or two bytes. They are the same. Please reread my
previous thread post so you can compare union using #define without struct
and union with struct here below.

union
{
struct _B
{
BYTE L;
BYTE H;
} B;
WORD W;
}

You can store two bytes into one word using pointer like this below. It
is identical to union above. The problem is that after C++ Compiler
converted C++ source code into x86 / non x86 machine language. It has extra
1-2 instructions because it needs to read memory address first before
accessing variable through pointer. Union does not have extra instructions.
It has only one instruction to acess variable instead of using pointer.
Union is the best choice.
Please explain why you claim that I made my mistake. Please show your
example of static_cast<> keyword. It is like to put pointer in
static_cast<>.

WORD W = 0;
BYTE L = (BYTE*)&W;
BYTE H = (BYTE*)&W+1;
*L = 0xFF;
*H = 0x20;

Bryan Parkoff


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-11-2007
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.

> [..]


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Bryan Parkoff
Guest
Posts: n/a
 
      12-11-2007
> 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


 
Reply With Quote
 
Fred Zwarts
Guest
Posts: n/a
 
      12-11-2007
"Bryan Parkoff" <(E-Mail Removed)> wrote in message news:475e085c$0$9912$(E-Mail Removed)...
>> 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 want to
> share two bytes into one word. I can modify one low byte at this time and
> high byte next time. Then word gets data from 2 bytes. You can define one
> array with two elements or two bytes. They are the same. Please reread my
> previous thread post so you can compare union using #define without struct
> and union with struct here below.
>
> union
> {
> struct _B
> {
> BYTE L;
> BYTE H;
> } B;
> WORD W;
> }
>
> You can store two bytes into one word using pointer like this below. It
> is identical to union above. The problem is that after C++ Compiler
> converted C++ source code into x86 / non x86 machine language. It has extra
> 1-2 instructions because it needs to read memory address first before
> accessing variable through pointer. Union does not have extra instructions.
> It has only one instruction to acess variable instead of using pointer.
> Union is the best choice.
> Please explain why you claim that I made my mistake. Please show your
> example of static_cast<> keyword. It is like to put pointer in
> static_cast<>.
>
> WORD W = 0;
> BYTE L = (BYTE*)&W;
> BYTE H = (BYTE*)&W+1;
> *L = 0xFF;
> *H = 0x20;
>
> Bryan Parkoff


The standard says that using an union in this way results in undefined behavior.
I don't know the rationale behind this, but I think that normally for a POD it is not a problem.
However, a union can be a complex thing and its members can be complex classes.
E.g., members which may be accessed only using their member functions. Even the assignment
operator may be overwritten, so that it is unclear what happens if you assign one member
of a union and read another member of a union.
Probably, this is the rationale behind the standard.
The same problems will show up in your second strategy if WORD and BYTE are complex classes.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
nameless Thank You Spam Namenlose Namenaktien Computer Support 8 10-03-2008 02:18 AM
Implementing a class of a nameless namespace outside the namespaceblock Juha Nieminen C++ 7 03-21-2007 02:19 PM
100 Years of E=mc2 : A critic is Nameless, Placeless, Identityless ? AJAY SHARMA C++ 0 09-26-2006 02:07 PM
100 Years of E=mc2 : A critic is Nameless, Placeless, Identityless ? AJAY SHARMA C Programming 0 09-26-2006 02:05 PM
union in struct without union name Peter Dunker C Programming 2 04-26-2004 07:23 PM



Advertisments