Probable Bug in Visual Studio

My friend Mohsin Saleem (no he doesn’t have a blog, so cannot link here) pointed out that he has seen a strange behavior on Visual Studio 6 for the following code.

  1. union u {
  2.     union u {
  3.         int i;
  4.         int j;
  5.     } a[20]; //Edit: Its not a[10]. Faisal, thanks for pointing it out.
  6.     int b[20]; 
  7. }u;
  8. int main()
  9. {
  10.     printf(“%d\n”, sizeof(u) );
  11.     printf(“%d\n”, sizeof(u.a));
  12.     return 0;
  13. }

Visual Studio returned

80
1600

Now that was amazing. I opened it in VS7. When made a .cpp file, it flagged a compile time error. When shifted to .c file, the same results 80, 1600.

Tried it on Dev-C++ and it worked according to what theory suggests. It returned

80
80

 

When change line 5 to “} a[10]”, Visual studio returned

40
400

Dev C++ Returned
40
40

With some other examples, I concluded that Visual Studio does this. sizeof(u) * (number of elements in a).

Don’t know whether this bug is reported or not. If it is, good. If not, the credit goes to Mohsin who first pointed this to me :).

 Edit2: Faisal also told me that he tried it on VS 2005 and its still a bug. Thanks to him in pointing out some blunders in my explanation as well.

Advertisements

3 Responses to Probable Bug in Visual Studio

  1. Ayaz says:

    You need to edit the edit again :). You have put two “http://” in the link to faisal’s blog.

    //Edit: Its not a[10]. Faisal, thanks for pointing

  2. Yes Ayaz :P. Corrected the link.

  3. Momekh says:

    salaams… i surfed to your blog through Adil Hafeez’s new blog. Must say, you are writing in true blogging spirit! With links to anything that is linkable! Great site… (Y)
    GodBless n good luck with MS!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: