about "anonymous union" problem 
Author Message
 about "anonymous union" problem

Hello..

I hava a problem about "anonymous union" in vc++.net.

Here is the code.
<<<<<<<<<<<<<<<
#include <stdio.h>
int main()
{
    union {
        char    logbuf[256];
    };
    int val;
    fprintf(stderr,"len2=%d\n",(char*)&val-logbuf);
    logbuf[180]=0;
    val=3;
    fprintf(stderr,"logbuf[180]=%d\n",logbuf[180]);

Quote:
}

Result:
<<<<<<<<<<<<<
len2=180
logbuf[180]=3
Press any key to continue
Why...?
It seems sizeof(union { char logbuf[256]; };)
  is less than 180..
It this correct?

Thank you...



Thu, 14 Oct 2004 13:06:25 GMT  
 about "anonymous union" problem
I can't reproduce this.  What compiler flags were you using?

BTW, the first printf isn't something you could rely on.  A really smart
optimizer could easily detect that parts of the union are unused and store
other variables in the same space.  I'm not certain that's happening here,
but it's possible (and possibly the bug.)

Jason Shirk
VC++ Compiler Team


Quote:
> Hello..

> I hava a problem about "anonymous union" in vc++.net.

> Here is the code.
> <<<<<<<<<<<<<<<
> #include <stdio.h>
> int main()
> {
>     union {
>         char    logbuf[256];
>     };
>     int val;
>     fprintf(stderr,"len2=%d\n",(char*)&val-logbuf);
>     logbuf[180]=0;
>     val=3;
>     fprintf(stderr,"logbuf[180]=%d\n",logbuf[180]);
> }

> Result:
> <<<<<<<<<<<<<
> len2=180
> logbuf[180]=3
> Press any key to continue

> Why...?
> It seems sizeof(union { char logbuf[256]; };)
>   is less than 180..
> It this correct?

> Thank you...



Thu, 14 Oct 2004 14:19:00 GMT  
 about "anonymous union" problem
The Compiler option ...

Copied From "Counfiguration Properties->C/C++/Command
Line"
<<<
/Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /FD /E
Hsc /RTC1 /MLd /YX"stdafx.h" /Fp".\Debug/aa.pch" /Fo".\Deb
ug/" /Fd".\Debug/" /W3 /nologo /c /ZI /TP

Copied From "Counfiguration Properties->Linker->Command
line"
<<<
/OUT:".\Debug/aa.exe" /INCREMENTAL /NOLOGO /DEBUG /PDB:".\
Debug/aa.pdb" /SUBSYSTEM:CONSOLE odbc32.lib odbccp32.lib  
kernel32.lib user32.lib gdi32.lib winspool.lib
comdlg32.lib advapi32.lib shell32.lib ole32.lib
oleaut32.lib uuid.lib odbc32.lib odbccp32.lib

Thank you.

Quote:
>-----Original Message-----
>I can't reproduce this.  What compiler flags were you
using?

>BTW, the first printf isn't something you could rely
on.  A really smart
>optimizer could easily detect that parts of the union

are unused and store
Quote:
>other variables in the same space.  I'm not certain

that's happening here,
Quote:
>but it's possible (and possibly the bug.)

>Jason Shirk
>VC++ Compiler Team



>> Hello..

>> I hava a problem about "anonymous union" in vc++.net.

>> Here is the code.
>> <<<<<<<<<<<<<<<
>> #include <stdio.h>
>> int main()
>> {
>>     union {
>>         char    logbuf[256];
>>     };
>>     int val;
>>     fprintf(stderr,"len2=%d\n",(char*)&val-logbuf);
>>     logbuf[180]=0;
>>     val=3;
>>     fprintf(stderr,"logbuf[180]=%d\n",logbuf[180]);
>> }

>> Result:
>> <<<<<<<<<<<<<
>> len2=180
>> logbuf[180]=3
>> Press any key to continue

>> Why...?
>> It seems sizeof(union { char logbuf[256]; };)
>>   is less than 180..
>> It this correct?

>> Thank you...

>.



Thu, 14 Oct 2004 14:47:08 GMT  
 about "anonymous union" problem
The previous project is converted from vc6.
I'v tried to new a new project from vc++.net,
   (Win32 Project,
       Application Type:console,
       Addidtion Option:empy project),
  and add the code to this project,
  use default setting and "Debug" configuration,
the result is same.

and I'v tried more..
add this line: memset(logbuf,1,256);

Quote:
>> {
>>     union {
>>         char    logbuf[256];
>>     };
>>     int val;
>>     fprintf(stderr,"len2=%d\n",(char*)&val-logbuf);

       memset(logbuf,1,256);

Quote:
>>     val=3;
>>     fprintf(stderr,"logbuf[180]=%d\n",logbuf[180]);
>> }

"Debug" -> "Start",

popup a dialog,

"Run-Time Check Failure #2-
Stack around the variable 'val' ws corrupted"

memset(logbuf,1,176);
is ok!

but when the third param of memset is greater than 176,
the dialog appears.

Thank you.
ps.
compiler option
/Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /Gm /E
Hsc /RTC1 /MLd /Fo"Debug/" /Fd"Debug/vc70.pdb" /W3 /nologo
 /c /Wp64 /ZI /TP
linker option
/OUT:"Debug/test.exe" /INCREMENTAL /NOLOGO /DEBUG /PDB:"De
bug/test.pdb" /SUBSYSTEM:CONSOLE /MACHINE:IX86  
kernel32.lib user32.lib gdi32.lib winspool.lib
comdlg32.lib advapi32.lib shell32.lib ole32.lib
oleaut32.lib uuid.lib odbc32.lib odbccp32.lib

Quote:
>-----Original Message-----
>I can't reproduce this.  What compiler flags were you
using?

>BTW, the first printf isn't something you could rely
on.  A really smart
>optimizer could easily detect that parts of the union

are unused and store
Quote:
>other variables in the same space.  I'm not certain

that's happening here,
Quote:
>but it's possible (and possibly the bug.)

>Jason Shirk
>VC++ Compiler Team



Thu, 14 Oct 2004 15:16:38 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. username change to "anonymous" in InternetOpen

2. allowed data types inside a "union"

3. remove() vrs fopen("""w")

4. Displaying binary data as ascii "1"'s and "0"'s

5. Looking for "Shroud"/"Obfus"

6. ""help with TSR""

7. Parse trees and "("")"

8. Error "free"-ing "malloc"-ed memory

9. Displaying binary data as ascii "1"'s and "0"'s

10. Anonymous union in VC++ 7

11. anonymous structs and unions : legal?

12. Attention "C"/"C++" gurus and "C" newbees

 

 
Powered by phpBB® Forum Software