|
Unions are not "type" safe since the same data element can be represented by two different types.
a programmer traped in a thugs body
|
|
|
|
|
ky_rerun wrote: Unions are not "type" safe since the same data element can be represented by two different types.
Well that is whole purpose of unions, plus there are many other data types which are not type safe.
-Prakash
|
|
|
|
|
ky_rerun wrote: Unions are not "type" safe since the same data element can be represented by two different types.
i guess you have a good point
check here http://www.weirdrealm.com/prog/cppstds.html[^]
it says,
Unions
Avoid unions, as they defeat strong type-checking and are very difficult to verify in a debugger.
-Prakash
|
|
|
|
|
Members of unions cannot have an incomplete type, type void, or function type. Therefore members cannot be an instance of the union but can be pointers to the union type being declared.
A union type declaration is a template only. Memory is not reserved until the variable is declared.
If a union of two types is declared and one value is stored, but the union is accessed with the other type, the results are unreliable. For example, a union of float and int is declared. A float value is stored, but the program later accesses the value as an int. In such a situation, the value would depend on the internal storage of float values. The integer value would not be reliable.
|
|
|
|
|
vallikumar wrote: A union type declaration is a template only. Memory is not reserved until the variable is declared.
? Not sure what you are trying to say.
Memory allocation (actually, setting a new high-water mark for the stack) for all stack variables is deferred until the start of the block they are declared in.
It sounds like you are trying to say that mem alloc is different for unions than for int's, struct's, ...
The size of a union is known at compile time (just like a struct) - it is the size of the largest member.
...cmk
Save the whales - collect the whole set
|
|
|
|
|
I general, the purpose of a union is to save space. Say you're storing data for a fruit and you have 3 types: Apples, Oranges and Pears. The representaion of these three fruits is distinct and a fruit can only be 1 of these types. Consider a struct that embodies this design:
struct Fruit
{
Type m_Type;
Apple m_Apple;
Orange m_Orange;
Pear m_Pear;
};
If we using this to save our fruit data to disc only one of the three types would actually contain valid data - The space occupied by the others is wasted. We can improve on this by using a union.
struct Fruit
{
Type m_Type;
union
{
Apple m_Apple;
Orange m_Orange;
Pear m_Pear;
} m_Data
};
With this format we don't waste as much space - the size of the union m_Data is the size of its largest member and all members occupy the same memory.
As the previous posters indicated - This saving comes at a price - Danger!
Steve
|
|
|
|
|
Stephen Hewitt wrote: This saving comes at a price - Danger!
What Danger!
Jesus Loves <marquee direction="up" height="40" scrolldelay="1" step="1" scrollamount="1" style="background:#aabbcc;border-bottom:thin solid 1px #6699cc">
--Owner Drawn
--Nothing special
--Defeat is temporary but surrender is permanent
--Never say quits
--Jesus is Lord
|
|
|
|
|
Hard to spot casts:
union Voodoo
{
int m_Number;
IUnknown* m_pUnk;
};
Voodoo v;
v.m_Number = 5;
v.m_pUnk->AddRef();
What happens here is effectively something like the following:
reinterpret_cast<IUnknown*>(5)->AddRef();
In fact this is an old "C" trick used to perform casts which are otherwise illegal.
Steve
|
|
|
|
|
|
the other simple method is u can process the items on the fly...
Stephen Hewitt wrote: union { Apple m_Apple; Orange m_Orange; Pear m_Pear; } m_Data
for eg as mentioned above u can use 3 types of fruits....
read the quantity of apples for each vendor eg. v1.apple v2.apple and perform the calculations...then v1.orange,v2.orange...(since u don't need v1.apple again)and do the calculations..in that way u can perform computations and re-use the memory
this is just one specific instance though
hope that helps
|
|
|
|
|
hi everybody
how to use function of CDC::GetCharWidth()HH
thanks in andvance!!
nothing
|
|
|
|
|
const char first = 'a';
const char last = 'z';
INT Sizes[last-first+1];
if ( dc.GetCharWidth(first, last, Sizes) )
{
// "Sizes" now contains the sizes!
}
Steve
|
|
|
|
|
I use a pointer of CInternetSession in a function.
When I use the function by button, all things are ok.
When I use it in a OnCommunication message function which recieves message from other device, there is a error 12006.
The source code is like that:
pIneternetSession->OpenURL(strUrl);
I am sure the strUrl is right.
Why?
|
|
|
|
|
can you print out and check what exactly strUrl Contains, i did a google and got
12006 INTERNET_UNRECOGNIZED_SCHEME
I am not sure though if its the rite error code meaning.
-Prakash
|
|
|
|
|
strUrl is 'http://www.sina.com.cn/'
|
|
|
|
|
OpenURL throws a CInternetException exception, can you catch and figure out what exactly is the error.
-Prakash
|
|
|
|
|
I put a button on the dialog.
After input the url into a textbox, push the button. Then the application will visit the web and download the webpage by OpenURL() function.
Until now all things are good.
Then I use a event to replace the button.
The event will be call by a RS232 input message.
I use the same function to visit the web.
Now there is a error CInternetException 12006.
I see the error code from dgbView and the debug version.
|
|
|
|
|
sorry it is difficult to diagonose the problem without understanding what those error code is.
if you have not solved the problem yet, i would suggest you to post a event (a user defined probably or WM_COMMAND that you were using before) and then call that api.
I am not sure, but may be it is conflicting with the data processing event call.
btw, are you calling the OpenURL from the same window that you were doing before?
-Prakash
|
|
|
|
|
I put the function into a new event (WM_USER+1).
Other fuction use it by PostMessage.
Now the error is CInternetSession 78.
|
|
|
|
|
The new error is :
Warnging:throwing CInternetException for error 87
OpenURL is not in the same window with the event, it is in a class which has a instance in the dialog.
|
|
|
|
|
I found the reason is that there is a space ' ' before the URL.
|
|
|
|
|
rushing wrote: I found the reason is that there is a space ' ' before the URL.
didnt I tell u that there could be some problem with the url , good that you identified.
I had a similar problem long back where a space in the parameter was causing a problem and it was resolved only after weeks of debuging.
-Prakash
|
|
|
|
|
I am lucky to meet you.
Happy Spring Festival!
|
|
|
|
|
rushing wrote: I am lucky to meet you.
:->, there are many more smarter ppl than me here in code-project, you would be even more luckier if you meet them too
-Prakash
|
|
|
|
|
Is there any way to capture the debug logging on a driver during bootup?
I know it can be done if you’re connected via serial port to another machine, but I’m looking for a method that is standalone.
The DebugView program from sysinternals only seems to work to capture bootup logging if you can successfully logon on open DebugView.
If the machine crashes before then, it seems you loose the logging.
I'm trying to debug a driver that gives me the blue screen of death as soon as I logon.
Top ten member of C++ Expert Exchange.
http://www.experts-exchange.com/Cplusplus
|
|
|
|