|
Basically the members of a union share the same memory address (while the members of a struct have distinct memory addresses).
The above implies, for instance:
union MyUnion
{
int i;
float f;
};
struct MyStruct
{
int i;
float f;
};
void main()
{
MyUnion u;
MyStruct s;
u.i = 5;
s.i = 5;
}
and, conceptually, sizeof(MyStruct)=sizeof(int)+sizeof(float) while sizeof(MyUnion)= max(sizeof(int), sizeof(float))
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
With unions all members start at the same base address so they overlap. With structures, the members are "stacked", i.e. each one starts in the byte after the previous one ends.
|
|
|
|
|
Alan Balkany wrote: each one starts in the byte after the previous one ends.
Too specific to be the right answer for this newbie. One poster said "conceptually" the sizes of the members sum, which is correct *CONCEPTUALLY*.
We should probably point out to the original poster that in a structure, the compiler is free to add padding after ANY member (including the last member) to insure the members align correctly. That is, a structure may contain unused bytes that are there only for alignment.
struct example{
char c;
long l;
};
printf( "sizeof(example) = %d", sizeof(example) );
In most compilers, this will NOT print "sizeof(example) = 5" as might be expected, since the compiler must put padding after c to insure that l aligns properly. In most modern compilers this will likely print "sizeof(example) = 8".
David
---------
Empirical studies indicate that 20% of the people drink 80% of the beer. With C++ developers, the rule is that 80% of the developers understand at most 20% of the language. It is not the same 20% for different people, so don't count on them to understand each other's code.
http://yosefk.com/c++fqa/picture.html#fqa-6.6
---------
|
|
|
|
|
question before answering : did you bother searching the web/docs/msdn/google/whatever before asking that dumb question asked and answered tons of times ?
the difference between unions and structures (and classes if in C++) is that all the members of the union share the same memory address. in other words, it means that they all start at the same address, and them, it also means that the size of the union equals the size of its largest member.
so, the other question which usually comes after such an answer is : "but what can it be used for, then ?".
good question Sherlock.
in fact, it's not used much in everyday programming, especially in management applications.
I used to use it when I was coding for hardware, when some component had to share the same physical address. so to represent such a material in the software, a union was exactly what was needed.
|
|
|
|
|
I think you are new to c++ so I suggest to see www.cplusplus.com and on that site you can see different union and other helpful info.
|
|
|
|
|
|
It was optional no coercion because I didnt use of link for that site.
|
|
|
|
|
While structure enables us treat a number of different variables stored at different in memory , a union enables us to treat the same space in memory as a number of different variables. That is a Union offers a way for a section of memory to be treated as a variable of one type on one occasion and as a different variable of a different type on another occasion.
Union allocates the memory equal to the maximum memory required by the member of the union but structure allocates the memory equal to the total memory required by the members.
Union allocates the memory equal to the maximum memory required by the member of the union but structure allocates the memory equal to the total memory required by the members.
Example:
union exforsys_t {
char c;
int i;
float f;
} exforsys;
Defines three elements:
exforsys.c
exforsys.i
exforsys.f
Each one with a different data type. Since all of them are referring to the same location in memory, the modification of one of the elements will affect the value of all of them. We cannot store different values in them independent from each other.
Sudhir Kumar
|
|
|
|
|
i am looking for unix file system emulator ...
can you let me know where can i find this?
hi
|
|
|
|
|
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
Unix systems are proprietary, so, you can't emulate, just can install them on the single hardwares they support.
but maybe you want a linux instead ?
if so, why don't you give a virtual machine a try ?
VMWare is really worth it !
|
|
|
|
|
Did you check VMWare supports Vista 64bit,Virtual Pc doesnt support 64bit but for 32bit it doesnt problem.
|
|
|
|
|
dunno. but i guess it also depends of the hardware of the VM host...
|
|
|
|
|
|
Hi all,
I have made two MFC Dialog Based Application, my problem is when either of a application is running second application does not run. Like if first application is running second application doesn't show up and vica-versa.
Can anbody tell me how to resolve this.
|
|
|
|
|
do you have a kind of "single instance" system which would forbid one application to start if a second instance already runs ?
that could then be a copy/paste mistake on the lock name (so that both application share the same lock, so behave as if they were the same)...?
|
|
|
|
|
Hey thanks you solved my problem.....
i had same mutex name for both the application
|
|
|
|
|
VCProgrammer wrote: Hey thanks you solved my problem.....
i had same mutex name for both the application
happy to heard about it
|
|
|
|
|
|
Did you try debugging your second application to see what's happening ?
Are you in some way trying to restrict your applications to one instance only (and that might interfere on the other one) ?
|
|
|
|
|
Start one application and then try to debug another application.Point out the return location.
Sudhir Kumar
|
|
|
|
|
You are talking about two separate applications, not two instances of same application, aren't you?
Debugger should tell you whats happening.
|
|
|
|
|
as I told him, I suspect he has 2 separate application, but with the same locking (single instance) system, so both apps behave like the same...
|
|
|
|
|
i have made dialog based application in mfc where there are lots of dialogs boxes. now i need to change the name of the application. what is the procedure to do that??
|
|
|
|
|
You can use SetWindowText()[^] to do that.
Nobody can give you wiser advice than yourself. - Cicero
.·´¯`·->Rajesh<-·´¯`·.
Codeproject.com: Visual C++ MVP
|
|
|
|