Anonymous union in VC++ 7 
Author Message
 Anonymous union in VC++ 7

How come anonymous union are allocated differently on the
stack then a tagged declared union ?
eg.
union foo
{
...
Quote:
} bar;

is allocated in a fixed position relative to the other
automatic vars in the stack,
whereas,
union
{
 int n;
 double d;
Quote:
};

can be allocated in different position relative to its
neighbour automatic vars ?

It doesn't matter where it gets allocated in the stack
but sometimes it gets allocated in the same spot as other
auto vars and causes the runtime stack check to throw an
exception.

Any suggestions ?  I am using the explicit tagged unions
now to avoid the problem but it is a pain to code that
way when I have to read from a byte stream and interprete
the data in a structured way.



Mon, 08 Nov 2004 23:29:25 GMT  
 Anonymous union in VC++ 7

Quote:
>How come anonymous union are allocated differently on the
>stack then a tagged declared union ?
>It doesn't matter where it gets allocated in the stack
>but sometimes it gets allocated in the same spot as other
>auto vars and causes the runtime stack check to throw an
>exception.

Do you have a small code snippet that illustrates this problem?

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
My address is altered to discourage junk mail.
Please post responses to the newsgroup thread,
there's no need for follow-up email copies.



Tue, 09 Nov 2004 00:28:34 GMT  
 Anonymous union in VC++ 7
I'm sorry I cannot reproduce the problem with a sample
code. I've tried recreating the problem with a test
project but cannot do so.  Specifically, the exception
thrown is 'Run-Time Check Failure #2 - Stack around the
variable '$S1' was corrupted.'

Here's a small snippet.
.
.
some code and local vars.
.
.
union
{
   PBYTE        pByte;  
   void*        pData_t;

Quote:
};

DWORD dwRecSize = MaxRecordSize;
pByte = new BYTE[ dwRecSize ];
.
.
code and local vars
.
.
.
delete [] pByte;
.
.
code and local vars.
.
.
.
Then exception thrown at exit of function.

But if I use
union foo
{
   PBYTE        pByte;  
   void*        pData_t;

Quote:
} bar;

DWORD dwRecSize = MaxRecordSize;
bar.pByte = new BYTE[ dwRecSize ];
.
.
delete [] bar.pByte;

then everything is fine.

Quote:
>-----Original Message-----
>>How come anonymous union are allocated differently on
the
>>stack then a tagged declared union ?

>>It doesn't matter where it gets allocated in the stack
>>but sometimes it gets allocated in the same spot as
other
>>auto vars and causes the runtime stack check to throw
an
>>exception.

>Do you have a small code snippet that illustrates this
problem?

>Dave
>--
>MVP VC++ FAQ: http://www.mvps.org/vcfaq
>My address is altered to discourage junk mail.
>Please post responses to the newsgroup thread,
>there's no need for follow-up email copies.
>.



Tue, 09 Nov 2004 00:55:49 GMT  
 Anonymous union in VC++ 7
Here is the listing with tagged union

_dStopDepth$122952 = -360
_idx$122951 = -340
_str$ = -328
_pSpeed$122944 = -316
_pParam$122940 = -304
_pDataTypeArray$122862 = -292
_uId$122789 = -280
_nDatasetCnt$ = -268
_nProgressCnt$ = -256
_nDepthCnt$ = -244
_uIdOld$ = -232
_pPulseArray$ = -220
_pIdxArray$ = -208
_lwRecord$ = -196
_dwRecSize$ = -180
_bar$ = -168  <------tagged union
_idx$122762 = -156
_pLogParam$ = -144

Here is the stack allocation listing with anonymous union
_dStopDepth$122954 = -360
_$S1$ = -360    <----anonymous union. same addr as above
_idx$122953 = -340
_str$ = -328
_pSpeed$122946 = -316
_pParam$122942 = -304
_pDataTypeArray$122864 = -292
_uId$122791 = -280
_nDatasetCnt$ = -268
_nProgressCnt$ = -256
_nDepthCnt$ = -244
_uIdOld$ = -232
_pPulseArray$ = -220
_pIdxArray$ = -208
_lwRecord$ = -196
_dwRecSize$ = -180
_idx$122762 = -156
_pLogParam$ = -144



Tue, 09 Nov 2004 01:01:09 GMT  
 Anonymous union in VC++ 7

Quote:
>I'm sorry I cannot reproduce the problem with a sample
>code. I've tried recreating the problem with a test
>project but cannot do so.

Pity.

Is there anything slightly out of the ordinary with your real code
that might be giving rise to this?

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
My address is altered to discourage junk mail.
Please post responses to the newsgroup thread,
there's no need for follow-up email copies.



Tue, 09 Nov 2004 02:16:01 GMT  
 Anonymous union in VC++ 7
Hi,Ed:

This looks like a compiler bug to me.  Does this happen with compiler
optimization turn on or not?  If it is turned on, you may want to disable
the optimization and to see if the problem reproduces.  Optimization
sometime could generate bad code.

You may also contact Microsoft Developer Support about this problem if
possible.

James

This posting is provided "AS IS" with no warranties, and confers no rights



Tue, 09 Nov 2004 08:15:57 GMT  
 Anonymous union in VC++ 7
Optimization was not turned on.  It was built in Debug
mode.  The code had been working under VC6 forever.  I am
just migrating to VC7.  
Also, I found another problem with code that had been
previously working under VC6 that's not working now.  But
this time it worked in Debug mode.  But what good is a
compiler if you can't use optimization ??
I think I'll just go back to VC6.  Way too many problems,
not to mention VC7 CByteArray.Serialize() also has a
bug...
I'm just too spooked now.  My code is commercial and I
like it to stay working without worrying about other
people's bugs, you know ??


Wed, 10 Nov 2004 00:09:47 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. about "anonymous union" problem

2. anonymous structs and unions : legal?

3. Anonymous unions

4. Anonymous unions in C with gcc?

5. Help: BC5 anonymous structures, unions

6. Anonymous unions in gcc

7. VC++ Version 5.0 anonymous variable bug.

8. Power of Unions ( surely union in C )

9. union in VC and GCC

10. VC++ 7.0 basic_string impl uses union

11. Anonymous object ??

12. anonymous struct

 

 
Powered by phpBB® Forum Software