473,782 Members | 2,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

union member

After compiling and running the following program

#include <stdio.h>
#include <string.h>

typedef union
{
int a;
char b[2048];
float c;
} Foo, *LPFOO;

static Foo Bloof;

int main(int argc, char *argv[])
{
Bloof.a=1;
printf("% d\n",(int)Bloof .a);
strcpy(Bloof.b, "Hello, World!");
printf("%s\n",B loof.b);
printf("% d\n",(int)Bloof .a);
Bloof.c=3.14159 ;
printf("% .7G\n",(float)B loof.c);
return 0;
}

the results produced are

1
Hello, World!
1819043144
3.14159

After Bloof.b has been made the active member of the union
what is the nature of the value, 1819043144, that the

printf("% d\n",(int)Bloof .a);

produces? Is it a numerical value related to the value, "Hello
World",
of the active union member Bloof.b or an address or just a random
number?

Robert Wishlaw

Apr 25 '07 #1
8 2323
<ir*******@gmai l.comschrieb im Newsbeitrag
news:11******** **************@ t39g2000prd.goo glegroups.com.. .
After compiling and running the following program

#include <stdio.h>
#include <string.h>

typedef union
{
int a;
char b[2048];
float c;
} Foo, *LPFOO;

static Foo Bloof;

int main(int argc, char *argv[])
{
Bloof.a=1;
printf("% d\n",(int)Bloof .a);
strcpy(Bloof.b, "Hello, World!");
printf("%s\n",B loof.b);
printf("% d\n",(int)Bloof .a);
Bloof.c=3.14159 ;
printf("% .7G\n",(float)B loof.c);
return 0;
}

the results produced are

1
Hello, World!
1819043144
3.14159

After Bloof.b has been made the active member of the union
what is the nature of the value, 1819043144, that the

printf("% d\n",(int)Bloof .a);

produces? Is it a numerical value related to the value, "Hello
World",
of the active union member Bloof.b or an address or just a random
number?
1819043144 == 0x6C6C6548, interpreted as ASCII it is "lleH", on an little
endian machine that would be the beginning of your string "Hello, World".
So apparently it is not purely random, but probaly undefined behavoir.

Bye, Jojo
Apr 25 '07 #2
In article <11************ **********@t39g 2000prd.googleg roups.com>
ir*******@gmail .com writes:
>After compiling and running the following program

#include <stdio.h>
#include <string.h>

typedef union
{
int a;
char b[2048];
float c;
} Foo, *LPFOO;
[snip]
>After Bloof.b has been made the active member of the union
what is the nature of the value, 1819043144, that the
There is no "active member" of a union. A union is a way to overlay
multiple uses on the same memory, but it does not track which use
you have used most recently. On the few instances that I need/use
a union for that sort of thing, I include it in a wrapper struct
with another field indicating what's in the union.

To be honest, the last time I recall using a union in professional
code was on a project that was started in 1994.
--
Drew Lawson http://www.furrfu.com/ dr**@furrfu.com

I only came in search of answers, never planned to sell my soul
I only came in search of something left that I could call my own
Apr 25 '07 #3
dr**@furrfu.com (Drew Lawson) writes:
There is no "active member" of a union.
The C standard does not use that exact term, but it does talk
about how the member of a union last stored into is the only
member whose value is not unspecified. One could reasonably
regard that member to be the "active member":

7 When a value is stored in a member of an object of union type,
the bytes of the object representation that do not
correspond to that member but do correspond to other members
take unspecified values, but the value of the union object
shall not thereby become a trap representation.
A union is a way to overlay multiple uses on the same memory,
but it does not track which use you have used most recently.
I don't see why the implementation could not do so; it's simply
that normal implementations do not.
--
Comp-sci PhD expected before end of 2007
Seeking industrial or academic position *outside California* in 2008
Apr 25 '07 #4
Ben Pfaff wrote:
>
dr**@furrfu.com (Drew Lawson) writes:
[...]
A union is a way to overlay multiple uses on the same memory,
but it does not track which use you have used most recently.

I don't see why the implementation could not do so; it's simply
that normal implementations do not.
Yes, I suppose it could if it really wanted to, by doing the equivalent
of turning the union into a struct consisting of the union plus a
tracker, and setting the tracker on every assignment to the union,
and checking the tracker on every reference to the union. However,
what would you expect the implementation do if it detected a mismatch?

On the other hand, I seem to recall that are allowed to access the
union via an array of unsigned chars, even if that was not the last
assignment. For example:

union examine_double_ format
{
double value;
unsigned char bytes[sizeof(double)];
};

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Apr 25 '07 #5
Kenneth Brody <ke******@spamc op.netwrites:
Ben Pfaff wrote:
>>
dr**@furrfu.com (Drew Lawson) writes:
[...]
A union is a way to overlay multiple uses on the same memory,
but it does not track which use you have used most recently.

I don't see why the implementation could not do so; it's simply
that normal implementations do not.

Yes, I suppose it could if it really wanted to, by doing the equivalent
of turning the union into a struct consisting of the union plus a
tracker, and setting the tracker on every assignment to the union,
and checking the tracker on every reference to the union. However,
what would you expect the implementation do if it detected a mismatch?
Provide a diagnostic to the programmer, who could then fix the
bug? It would be great if programming tools could report this
sort of standard violation.
On the other hand, I seem to recall that are allowed to access the
union via an array of unsigned chars, even if that was not the last
assignment. [...]
Yes, there are some special exceptions to the rule.
--
"Given that computing power increases exponentially with time,
algorithms with exponential or better O-notations
are actually linear with a large constant."
--Mike Lee
Apr 25 '07 #6
Kenneth Brody wrote On 04/25/07 14:13,:
Ben Pfaff wrote:
>>dr**@furrfu.c om (Drew Lawson) writes:

[...]
>>>A union is a way to overlay multiple uses on the same memory,
but it does not track which use you have used most recently.

I don't see why the implementation could not do so; it's simply
that normal implementations do not.


Yes, I suppose it could if it really wanted to, by doing the equivalent
of turning the union into a struct consisting of the union plus a
tracker, and setting the tracker on every assignment to the union,
and checking the tracker on every reference to the union. [...]
I think that could produce false positives:

union { int i; double d; } u;
u.i = 42; /* tracker says "int" */
sscanf("42.42", "%lf", &u.d);
printf ("%f\n", u.d); /* tracker mismatch? */

The assignment to u.d takes place inside sscanf(), where
it's not known that the pointer is to a union member as
opposed to a free-standing double.

--
Er*********@sun .com
Apr 25 '07 #7
Ben Pfaff <bl*@cs.stanfor d.eduwrites:
Kenneth Brody <ke******@spamc op.netwrites:
>Ben Pfaff wrote:
>>dr**@furrfu.com (Drew Lawson) writes:
[...]
>A union is a way to overlay multiple uses on the same memory,
but it does not track which use you have used most recently.

I don't see why the implementation could not do so; it's simply
that normal implementations do not.

Yes, I suppose it could if it really wanted to, by doing the equivalent
of turning the union into a struct consisting of the union plus a
tracker, and setting the tracker on every assignment to the union,
and checking the tracker on every reference to the union. However,
what would you expect the implementation do if it detected a mismatch?

Provide a diagnostic to the programmer, who could then fix the
bug? It would be great if programming tools could report this
sort of standard violation.
Providing a diagnostic *to the programmer* implies a compile-time
message, which isn't always possible. Where it is possible, compilers
can already do it without adding a tracker to the union itself (or
rather by implementing the tracker within the compiler).

Detecting it at run time would be tricky, assuming you want to
maintain compatibility. I suppose you could store the tracker in what
would otherwise be padding bytes at the end of the union (you can't
put it at the beginning because the explicit members all have to have
an offset of 0).

That leaves the question of what to do if you detect an error at run
time. You could just abort the program, which is what often happens
for things like null pointer dereferences. Or you could raise a
signal.

Then again, if a programmer wants to use something like what Pascal
calls a variant record, he can implement it himself.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Apr 25 '07 #8
Keith Thompson <ks***@mib.orgw rites:
Ben Pfaff <bl*@cs.stanfor d.eduwrites:
>Kenneth Brody <ke******@spamc op.netwrites:
>>Ben Pfaff wrote:
dr**@furrfu.com (Drew Lawson) writes:
[...]
A union is a way to overlay multiple uses on the same memory,
but it does not track which use you have used most recently.

I don't see why the implementation could not do so; it's simply
that normal implementations do not.

Yes, I suppose it could if it really wanted to, by doing the equivalent
of turning the union into a struct consisting of the union plus a
tracker, and setting the tracker on every assignment to the union,
and checking the tracker on every reference to the union. However,
what would you expect the implementation do if it detected a mismatch?

Provide a diagnostic to the programmer, who could then fix the
bug? It would be great if programming tools could report this
sort of standard violation.

Detecting it at run time would be tricky, assuming you want to
maintain compatibility. I suppose you could store the tracker in what
would otherwise be padding bytes at the end of the union (you can't
put it at the beginning because the explicit members all have to have
an offset of 0).
I was thinking of a mechanism like the one that Valgrind uses to
track memory initialization state; that is, data completely in an
address space logically separate from that accessible by the
program.

Not every implementation has to be one useful for running
programs at a practical speed; I'm happy to do some testing of my
software under implementations that run with 100X overhead, if
they find important bugs.
--
Comp-sci PhD expected before end of 2007
Seeking industrial or academic position *outside California* in 2008
Apr 25 '07 #9

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

Similar topics

2
2532
by: Jeff Massung | last post by:
I am having a syntax issue that I hope someone else here knows how to rectify... I am loading an INI file and have a simple function to load values from it. The function is overloaded with the different return types int, double or string. A structure is defined: typedef struct INI_KEY { char strCategory; char strKey;
5
5231
by: Ricky Lung | last post by:
struct Foo { union { int& i; float& j; }; Foo(int& val) : i((int&)val), j((float&)val) {} };
10
1349
by: rohit | last post by:
Hi, Iam confused as to when is the memory freed in this program. #include <stdlib.h> #include <stdio.h> #include <string.h> union test{ char *i;
3
2038
by: Default User | last post by:
After a discussion on c.l.c++ with Ron Natalie, I'm now unclear as to the status of reading data stored as one union member, then accessed with a different one, in C99. I don't have the latest standard, as I don't work in it currently, so I use the draft standard. My reading was that this is implementation-defined, based on this: 6.5.2.2
1
2341
by: Michael B Allen | last post by:
Consider the following structure with a union: struct shape { int type; union { struct circle c; struct square s; struct polydoodle p; } u; };
0
1181
by: S.Tobias | last post by:
In DR#236 (recently brought about in "contiguity of arrays" thread) http://www.open-std.org/JTC1/SC22/WG14/www/docs/dr_236.htm at the end ("Committee Discussion") it has been said that Example 2 is invalid, because it violates 6.5#7. I kindly ask you to explain exactly why. My (counter) arguments: 6.5#7 talks about accessing a *value*, whereas in Example 2 a
4
7134
by: Jeff Mallett | last post by:
VC++.NET gave me Compiler Error C2621, which states, "A union member cannot have a copy constructor." Yikes, that can't be true! As I understand it, *all* class objects have copy constructors, since if they aren't explicit, one is implicitly generated. If this were true, class objects could not be members of a union, but I know they can be. I assume what is meant is that a union member can't have
0
9639
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
9479
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
10311
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
10146
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
7492
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6733
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();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.