473,597 Members | 2,285 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Dec 10 '07 #1
18 6076
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
Dec 10 '07 #2
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.

Dec 10 '07 #3
On Dec 11, 5:49 am, "Bryan Parkoff" <nos...@nospam. comwrote:
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
Dec 10 '07 #4
>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;

}
Dec 10 '07 #5
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
Dec 11 '07 #6
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<key word. 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
Dec 11 '07 #7
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
Dec 11 '07 #8
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
Dec 11 '07 #9
"Bryan Parkoff" <no****@nospam. comwrote in message news:47******** **************@ roadrunner.com. ..
>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<key word. 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.
Dec 11 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5045
by: Peter Dunker | last post by:
Hi, I will write ANSI C89. Is the following struct defenition correct ? I wrote it with VC6(Windows IDE) and at first no Problem. As I changed a compiler switch to 'no language extension', the compiler said that the union has no name. Is it right that in ANSI C the union must be named inside this kind of structure ?
67
10698
by: S.Tobias | last post by:
I would like to check if I understand the following excerpt correctly: 6.2.5#26 (Types): All pointers to structure types shall have the same representation and alignment requirements as each other. All pointers to union types shall have the same representation and alignment requirements as each other. Does it mean that *all* structure (or union) types have the same alignment? Eg. type
6
2684
by: S.Tobias | last post by:
I'm trying to understand how structure type completion works. # A structure or union type of unknown # content (as described in 6.7.2.3) is an incomplete type. It # is completed, for all declarations of that type, by ^^^ # declaring the same structure or union tag with its defining # content later in the same scope. ^^^^^ (6.2.5#23)
21
2441
by: hermes_917 | last post by:
I want to use memcpy to copy the contents of one struct to another which is a superset of the original struct (the second struct has extra members at the end). I wrote a small program to test this, and it seems to work fine, but are there any cases where doing something like this could cause any problems? Here's the small program I wrote to test this: #include <stdio.h>
8
1672
by: Mike | last post by:
The following struct, DataStruct, is only part of a larger one that contains additional fields and arrays. I need the explicit layout because this struct is really a union, where some of the missing fields and arrays overlap. What's shown here, though, is sufficient for explaining the error. 290 bytes of data come from a serial device and is to be placed in this struct. Hence, I want this struct to be 290 bytes in size, and, if I'm...
2
2217
by: Walter Deodiaus | last post by:
I have typedef struct { union _union{ .... struct { int i; }u1; .... }Union; } Struct ;
32
8823
by: Weiguang Shi | last post by:
Hi, Is there a tool that, given a struct definition, generates a function that parses binary data of this struct and a command that can be used to construct binary data according to user-specified values for the fields of this struct? Thank you. Wei
4
2573
by: Michael Brennan | last post by:
I have a menu_item structure containing an union. func is used if the menu item should use a callback, and submenu if a popupmen should be shown. struct menu_item { enum { function, popup } type; union { int (*func)(int); struct menu_item *submenu; } action;
10
2234
by: Raman | last post by:
Hi All, Is it valid: struct test{ }; I mean, Can we have a struct containing no members? Is this a an
0
7971
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
7893
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8276
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8381
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8040
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8259
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5436
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
1495
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1243
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.