When I compiled 21.string.io.cpp using Intel C++ 9.1 I've found that
a number of rw_asserts is different than with using MSVC++ 7.1.
I investigated this and detected the problem in UserInt::to_char()
method.
Incubator\stdcxx\trunk\tests\include\rw_char.h
UserChar UserInt::to_char () const {
const UserChar tmp = { 0, i_ };
return tmp;
}
Assembler code, generated by ICL 9.1:
UserChar to_char () const {
00439A44 push ebp
00439A45 mov ebp,esp
00439A47 push esi
00439A48 mov dword ptr [ebp-4],ecx
const UserChar tmp = { 0, i_ };
00439A4B fldz
00439A4D mov eax,dword ptr [ebp+8]
00439A50 fstp qword ptr [eax]
return tmp;
00439A52 mov eax,dword ptr [ebp+8]
00439A55 leave
00439A56 ret 4
As I see tmp.c is not initialized by value this->i_ and as a result
it contains a random value.
I tried to remove the const clause situated before UserChar, but
nothing was changed.
UserChar UserInt::to_char () const {
UserChar tmp = { 0, i_ };
return tmp;
}
Then I tried the following variant and it was compiled without the bug:
UserChar to_char () const {
UserChar tmp;
tmp.f = 0;
tmp.c = static_cast<unsigned char>(i_);
return tmp;
}
I attached the test case to illustrate the problem.
Farid.
#include <cassert>
struct UserChar
{
long double f;
unsigned char c;
};
struct UserInt
{
int i_;
UserChar to_char () const
{
const UserChar tmp = { 0, i_ };
return tmp;
}
UserChar to_char2 () const
{
UserChar tmp = { 0, i_ };
return tmp;
}
UserChar to_char3 () const
{
UserChar tmp = { 0 };
tmp.c = i_;
return tmp;
}
};
int main(int argc, char* argv[])
{
const char TEST_CHAR = 'a';
UserInt ui = { TEST_CHAR };
UserChar uc = ui.to_char ();
assert (TEST_CHAR == uc.c);
UserChar uc2 = ui.to_char2 ();
assert (TEST_CHAR == uc2.c);
UserChar uc3 = ui.to_char3 ();
assert (TEST_CHAR == uc3.c);
return 0;
}