|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On Dec 11, 5:49 am, "Bryan Parkoff" <nos...@nospam.com> 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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
>> 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; } |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
> 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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|