473,396 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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",Bloof.b);
printf("% d\n",(int)Bloof.a);
Bloof.c=3.14159;
printf("% .7G\n",(float)Bloof.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 2294
<ir*******@gmail.comschrieb im Newsbeitrag
news:11**********************@t39g2000prd.googlegr oups.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",Bloof.b);
printf("% d\n",(int)Bloof.a);
Bloof.c=3.14159;
printf("% .7G\n",(float)Bloof.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**********************@t39g2000prd.googlegroups .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******@spamcop.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.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. [...]
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.stanford.eduwrites:
Kenneth Brody <ke******@spamcop.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_Keith) 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.orgwrites:
Ben Pfaff <bl*@cs.stanford.eduwrites:
>Kenneth Brody <ke******@spamcop.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
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...
5
by: Ricky Lung | last post by:
struct Foo { union { int& i; float& j; }; Foo(int& val) : i((int&)val), j((float&)val) {} };
10
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
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...
1
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
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...
4
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,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.