PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > nameless struct / union
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
nameless struct / union

Réponse
 
LinkBack Outils de la discussion
Vieux 10/12/2007, 18h49   #1
Bryan Parkoff
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut nameless struct / union

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


  Réponse avec citation
Vieux 10/12/2007, 20h41   #2
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: nameless struct / union

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


  Réponse avec citation
Vieux 10/12/2007, 21h20   #3
Rolf Magnus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: nameless struct / union

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.

  Réponse avec citation
Vieux 10/12/2007, 21h34   #4
Craig Scott
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: nameless struct / union

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
  Réponse avec citation
Vieux 10/12/2007, 22h35   #5
Bryan Parkoff
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: nameless struct / union

>> 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;

}


  Réponse avec citation
Vieux 11/12/2007, 01h33   #6
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: nameless struct / union

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


  Réponse avec citation
Vieux 11/12/2007, 03h47   #7
Bryan Parkoff
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: nameless struct / union

> 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


  Réponse avec citation
Vieux 11/12/2007, 05h54   #8
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: nameless struct / union

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


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h49.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14793 seconds with 16 queries