473,327 Members | 1,930 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,327 software developers and data experts.

Anonymous Uniouns

Raj
Hi friends....

Could any one tell me atleast one application of anonymous uniouns in
C++?

Thank you

Feb 1 '06 #1
5 1270
Raj wrote:
Could any one tell me atleast one application of anonymous uniouns in
C++?


Keeping interns busy.

Luke

Feb 1 '06 #2
Homework questions that people on a newsgroup would rather not answer
for you?

Feb 1 '06 #3
On 31 Jan 2006 22:14:31 -0800, "Raj" <ra********@gmail.com> wrote:
Hi friends....

Could any one tell me atleast one application of anonymous uniouns in
C++?

Thank you


Transmogrifying normal unions into state-of-the-art maintainer traps
Feb 1 '06 #4
Tom
On 31 Jan 2006 22:14:31 -0800, "Raj" <ra********@gmail.com> wrote:
Hi friends....

Could any one tell me atleast one application of anonymous uniouns in
C++?

Thank you


Very interesting. I've never even heard of them before.

http://windowssdk.msdn.microsoft.com...9eb28358ad.asp

So ... I played around with them a bit and modified the above example
program (see below). Note how the printed out value of x changes.
Demonstrating that the union's variables are sharing the same memory
space. My C++ I/O skills are still lacking -- so I used my good ol'
friend printf().

What good is it? It allows a list of variables of varying type to
share the memory space of the longest type. Thus conserving memory
resources. Now, let's make everyone cringe. Just buy a gig of ram and
declare a bunch of global variables and don't worry about conserving
memory. :))

// anonymous_unions.cpp
// compile with: /EHsc
#include <iostream>
#include <stdio.h>

using namespace std;
int main() {
union {
int d;
char *f;
double x;
};

f = "test";
x = 999999999999.999;
printf("x = %18.3f\n", x);
d = INT_MAX;
cout << "INT_MAX = " << d << endl;
printf("x = %18.3f\n", x);
f = "here's a really long string stored in stack memory and not
allocated until run time; however, the pointer address resides in the
anonymous union";
cout << f << endl;
cout << " sizeof(int): " << sizeof(int) << endl;
cout << "sizeof(double): " << sizeof(double) << endl;
cout << " sizeof(char*): " << sizeof(char*) << endl;
}
// Program Output
// x = 999999999999.999
// INT_MAX = 2147483647
// x = 999999930368.000
// here's a really long string stored in stack memory and not
allocated until run time; however, the pointer address resides in the
anonymous union
// sizeof(int): 4
// sizeof(double): 8
// sizeof(char*): 4
Feb 1 '06 #5
Raj wrote:
Hi friends....

Could any one tell me atleast one application of anonymous uniouns in
C++?

Thank you


Constructing (almost) variant data type (almost) without dynamic memory
allocation.

Ben
Feb 1 '06 #6

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

Similar topics

0
by: Carlos Ribeiro | last post by:
I thought about this problem over the weekend, after long hours of hacking some metaclasses to allow me to express some real case data structures as Python classes. I think that this is something...
0
by: Cordell Lawrence | last post by:
Okay guys, We are wondering if this is a bug in Framework 2.0.40607 and looking for some clarification on the issue. Take a look at the folowing code. public delegate bool BoundryTest(int...
3
by: anonymous | last post by:
I believe I ran into an interesting way to create memory leaks in C# 2.0 using anymous delegates. Here is a sample of the code in question. private void Handle_Event(object sender, EventArgs e)...
4
by: Frankie | last post by:
I have just gotten up to speed on what anonymous methods are (syntax, capabilities, etc), and how they can be used with /called via delegates. What I am wondering is... 1. Are they only/mostly...
3
by: Robert Howells | last post by:
Is there any such thing as an "anonymous delegate"? I understand what an anonymous method is, but I'm unclear of the concept of anonymous delegate (if it is even a valid term to be using). Is an...
2
by: Tony | last post by:
Hello! Here I have some text from a book I read. It says: "An interesting point to note concerning anonymous methods is that they are effectively local to the code block that contains them, and...
0
by: Peter Duniho | last post by:
On Mon, 01 Sep 2008 16:14:10 -0700, Blip <blip@krumpli.comwrote: Briefly, an anonymous method is exactly that: a method without a name. When you use the "delegate" keyword to declare an...
3
by: shapper | last post by:
Hello, On my web site I have a property, Visitor, which is available for Anonymous users: public class Visitor { public CultureInfo Culture { get; set; } public List<GuidPolls { get; set;...
22
by: Luna Moon | last post by:
I am reading the book "C++ Annotations", and here is a quote from the book: Namespaces can be defined without a name. Such a namespace is anonymous and it restricts the visibility of the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.