Search

Multiple inheritance - wrong sizeof by R. Green

Closed
as Won't Fix Help for as Won't Fix

1
0
Sign in
to vote
Type: Bug
ID: 101525
Opened: 11/24/2005 2:27:10 AM
Access Restriction: Public
0
Workaround(s)
0
User(s) can reproduce this bug
Sizeof gives wrong (or weird ?) result in some cases of multiple inheritance including empty parent structures.
Details (expand)
Product Language
English
Version
Visual Studio 2005
Category
Language/Compiler
Operating System
Windows XP Professional
Operating System Language
US English
Steps to Reproduce
Compile the code below.

#include <stdio.h>
#pragma pack(1)

struct S00
{
};
struct S01
{
};
struct S02
{
int m_;
};



struct S10 : S01
{
int m_;
};
struct S11 : S02
{
};


struct S20 : S00, S01
{
int m_;
};
struct S22 : S00, S10
{
};
struct S21 : S00, S11
{
};




int main(int argc, char* argv[])
{
printf("sizeof(S10) = %d\nsizeof(S11) = %d\n",
sizeof(S10), sizeof(S11));

printf("sizeof(S20) = %d\nsizeof(S21) = %d\nsizeof(S22) = %d\n",
sizeof(S20), sizeof(S21), sizeof(S22));

return 0;
}

Actual Results
Actual program output:

sizeof(S10) = 4
sizeof(S11) = 4
sizeof(S20) = 5
sizeof(S21) = 4
sizeof(S22) = 5
Expected Results
program output should be:

sizeof(S10) = 4
sizeof(S11) = 4
sizeof(S20) = 4
sizeof(S21) = 4
sizeof(S22) = 4

I.e. all the structures should have the same size of 4 bytes.
File Attachments
0 attachments
Sign in to post a comment.
Posted by Microsoft on 3/1/2006 at 6:06 PM
Hello,

Upon further investigation, we've determined that fixing this would introduce a breaking change in the Visual C++ compiler's object model output. This would end up causing binary breaking changes among APIs, followed by a wave of versioning problems. Given that the existing behavior is conformant, we cannot realistically motivate a severe binary breaking change.

I'm sorry to report this as the resolution, but I do want to express gratitude to you for reporting this issue to us.

Brandon Bray
Visual C++ Compiler Program Manager
Posted by Microsoft on 3/1/2006 at 5:58 PM
Hello,

Upon further investigation, we've determined that fixing this would introduce a breaking change in the Visual C++ compiler's object model output. This would end up causing binary breaking changes among APIs, followed by a wave of versioning problems. Given that the existing behavior is conformant, we cannot realistically motivate a severe binary breaking change.

I'm sorry to report this as the resolution, but I do want to express gratitude to you for reporting this issue to us.

Brandon Bray
Visual C++ Compiler Program Manager
Posted by Microsoft on 1/5/2006 at 6:30 PM
The Microsoft Sub-status is now "Investigating"
Posted by Microsoft on 12/2/2005 at 11:50 AM
Hello,
The issues you see here are instances of the zero-size base class optimization not being applied. We do know of bugs when the optimization is incorrectly applied. In the cases shown in the code here, the optimization is not applied. As such, the results technically do conform to the C++ standard. I'm changing the issue type for this report to suggestion, since we should consider improving this optimization.

Our development team will look at this report and see whether we can address it in the short term.

Thanks for providing feedback!
Brandon Bray
Visual C++ Compiler Program Manager
Posted by R. Green on 11/24/2005 at 2:41 AM
Comeaou C++ compiler gives correct result.
Sign in to post a workaround.