我有以下代码在 Debug模式下可以正常运行,但在 Release模式下会产生错误的输出。
该代码很简单,它只是创建一个4x4的单位矩阵并将其写入标准输出。

#include <iostream>
struct mat4{
    float a00; float a01; float a02;  float a03;
    float a10; float a11; float a12;  float a13;
    float a20; float a21; float a22;  float a23;
    float a30; float a31; float a32;  float a33;
};

mat4 make_identity(){
    mat4 v;
    v.a00 = 1;  v.a01 = 0;      v.a02 = 0;      v.a03 = 0;
    v.a10 = 0;  v.a11 = 1;      v.a12 = 0;      v.a13 = 0;
    v.a20 = 0;  v.a21 = 0;      v.a22 = 1;      v.a23 = 0;
    v.a30 = 0;  v.a31 = 0;      v.a32 = 0;      v.a33 = 1;
    return v;
}



int main(){
    mat4 Rm = make_identity();
    std::cout << Rm.a00 << " " <<  Rm.a01 << " " << Rm.a02 << " " <<  Rm.a03 << std::endl;
    std::cout << Rm.a10 << " " <<  Rm.a11 << " " << Rm.a12 << " " <<  Rm.a13 << std::endl;
    std::cout << Rm.a20 << " " <<  Rm.a21 << " " << Rm.a22 << " " <<  Rm.a23 << std::endl;
    std::cout << Rm.a30 << " " <<  Rm.a31 << " " << Rm.a32 << " " <<  Rm.a33 << std::endl;
    system("PAUSE");
    return  0;
}

Release模式下的输出在每次运行时以及在不同的计算机上都是相同的,也就是说,它不是随机的。
1 1.4013e-045 0 0
0 0 1.1547e+014 0
0 7.63318e-038 3.46682e-039 0
0 0 0 0

我编译时启用了完全优化(/ Ox)。
请注意,如果我禁用了内联函数扩展(/ Ob0),则代码将正常工作。

我真的希望它不是编译器错误。

您有什么建议可以帮助我理解问题吗?

我的平台是:
Windows 7的
视觉工作室2010 10.0.30319.1

在 Release模式下生成的汇编代码为
int main(){
000000013F2F1000  sub         rsp,68h
    mat4 Rm = make_identity();
000000013F2F1004  movaps      xmm1,xmmword ptr [__xi_z+90h (13F2F2260h)]
000000013F2F100B  movaps      xmm0,xmmword ptr [__xi_z+80h (13F2F2250h)]
    std::cout << Rm.a00 << " " <<  Rm.a01 << " " << Rm.a02 << " " <<  Rm.a03 << std::endl;
000000013F2F1012  mov         rcx,qword ptr [__imp_std::cout (13F2F20B8h)]
000000013F2F1019  movss       dword ptr [rsp+30h],xmm1
000000013F2F101F  movss       dword ptr [rsp+20h],xmm0
000000013F2F1025  movaps      xmm1,xmmword ptr [__xi_z+0B0h (13F2F2280h)]
000000013F2F102C  movaps      xmm0,xmmword ptr [__xi_z+0A0h (13F2F2270h)]
000000013F2F1033  movss       dword ptr [rsp+50h],xmm1
000000013F2F1039  movss       xmm1,dword ptr [rsp+20h]
000000013F2F103F  movss       dword ptr [rsp+40h],xmm0
000000013F2F1045  call        qword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (13F2F20A0h)]
000000013F2F104B  lea         rdx,[__xi_z+48h (13F2F2218h)]
000000013F2F1052  mov         rcx,rax
000000013F2F1055  call        std::operator<<<std::char_traits<char> > (13F2F12C0h)
000000013F2F105A  mov         rcx,rax
000000013F2F105D  movss       xmm1,dword ptr [rsp+24h]
000000013F2F1063  call        qword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (13F2F20A0h)]
000000013F2F1069  lea         rdx,[__xi_z+44h (13F2F2214h)]
000000013F2F1070  mov         rcx,rax
000000013F2F1073  call        std::operator<<<std::char_traits<char> > (13F2F12C0h)
000000013F2F1078  mov         rcx,rax
000000013F2F107B  movss       xmm1,dword ptr [rsp+28h]
000000013F2F1081  call        qword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (13F2F20A0h)]
000000013F2F1087  lea         rdx,[__xi_z+40h (13F2F2210h)]
000000013F2F108E  mov         rcx,rax
000000013F2F1091  call        std::operator<<<std::char_traits<char> > (13F2F12C0h)
000000013F2F1096  mov         rcx,rax
000000013F2F1099  movss       xmm1,dword ptr [rsp+2Ch]
000000013F2F109F  call        qword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (13F2F20A0h)]
000000013F2F10A5  mov         rdx,qword ptr [__imp_std::endl (13F2F2088h)]
000000013F2F10AC  mov         rcx,rax
000000013F2F10AF  call        qword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (13F2F2098h)]
    std::cout << Rm.a10 << " " <<  Rm.a11 << " " << Rm.a12 << " " <<  Rm.a13 << std::endl;
000000013F2F10B5  mov         rcx,qword ptr [__imp_std::cout (13F2F20B8h)]
000000013F2F10BC  movss       xmm1,dword ptr [rsp+30h]
000000013F2F10C2  call        qword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (13F2F20A0h)]
000000013F2F10C8  lea         rdx,[__xi_z+54h (13F2F2224h)]
000000013F2F10CF  mov         rcx,rax
000000013F2F10D2  call        std::operator<<<std::char_traits<char> > (13F2F12C0h)
000000013F2F10D7  mov         rcx,rax
000000013F2F10DA  movss       xmm1,dword ptr [rsp+34h]
000000013F2F10E0  call        qword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (13F2F20A0h)]
000000013F2F10E6  lea         rdx,[__xi_z+50h (13F2F2220h)]
000000013F2F10ED  mov         rcx,rax
000000013F2F10F0  call        std::operator<<<std::char_traits<char> > (13F2F12C0h)
000000013F2F10F5  mov         rcx,rax
000000013F2F10F8  movss       xmm1,dword ptr [rsp+38h]
000000013F2F10FE  call        qword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (13F2F20A0h)]
000000013F2F1104  lea         rdx,[__xi_z+4Ch (13F2F221Ch)]
000000013F2F110B  mov         rcx,rax
000000013F2F110E  call        std::operator<<<std::char_traits<char> > (13F2F12C0h)
000000013F2F1113  mov         rcx,rax
000000013F2F1116  movss       xmm1,dword ptr [rsp+3Ch]
000000013F2F111C  call        qword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (13F2F20A0h)]
000000013F2F1122  mov         rdx,qword ptr [__imp_std::endl (13F2F2088h)]
000000013F2F1129  mov         rcx,rax
000000013F2F112C  call        qword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (13F2F2098h)]
    std::cout << Rm.a20 << " " <<  Rm.a21 << " " << Rm.a22 << " " <<  Rm.a23 << std::endl;
000000013F2F1132  mov         rcx,qword ptr [__imp_std::cout (13F2F20B8h)]
000000013F2F1139  movss       xmm1,dword ptr [rsp+40h]
000000013F2F113F  call        qword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (13F2F20A0h)]
000000013F2F1145  lea         rdx,[__xi_z+60h (13F2F2230h)]
000000013F2F114C  mov         rcx,rax
000000013F2F114F  call        std::operator<<<std::char_traits<char> > (13F2F12C0h)
000000013F2F1154  mov         rcx,rax
000000013F2F1157  movss       xmm1,dword ptr [rsp+44h]
000000013F2F115D  call        qword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (13F2F20A0h)]
000000013F2F1163  lea         rdx,[__xi_z+5Ch (13F2F222Ch)]
000000013F2F116A  mov         rcx,rax
000000013F2F116D  call        std::operator<<<std::char_traits<char> > (13F2F12C0h)
000000013F2F1172  mov         rcx,rax
000000013F2F1175  movss       xmm1,dword ptr [rsp+48h]
000000013F2F117B  call        qword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (13F2F20A0h)]
000000013F2F1181  lea         rdx,[__xi_z+58h (13F2F2228h)]
000000013F2F1188  mov         rcx,rax
000000013F2F118B  call        std::operator<<<std::char_traits<char> > (13F2F12C0h)
000000013F2F1190  mov         rcx,rax
000000013F2F1193  movss       xmm1,dword ptr [rsp+4Ch]
000000013F2F1199  call        qword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (13F2F20A0h)]
000000013F2F119F  mov         rdx,qword ptr [__imp_std::endl (13F2F2088h)]
000000013F2F11A6  mov         rcx,rax
000000013F2F11A9  call        qword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (13F2F2098h)]
    std::cout << Rm.a30 << " " <<  Rm.a31 << " " << Rm.a32 << " " <<  Rm.a33 << std::endl;
000000013F2F11AF  movss       xmm1,dword ptr [rsp+50h]
000000013F2F11B5  mov         rcx,qword ptr [__imp_std::cout (13F2F20B8h)]
000000013F2F11BC  call        qword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (13F2F20A0h)]
000000013F2F11C2  lea         rdx,[__xi_z+6Ch (13F2F223Ch)]
000000013F2F11C9  mov         rcx,rax
000000013F2F11CC  call        std::operator<<<std::char_traits<char> > (13F2F12C0h)
000000013F2F11D1  mov         rcx,rax
000000013F2F11D4  movss       xmm1,dword ptr [rsp+54h]
000000013F2F11DA  call        qword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (13F2F20A0h)]
000000013F2F11E0  lea         rdx,[__xi_z+68h (13F2F2238h)]
000000013F2F11E7  mov         rcx,rax
000000013F2F11EA  call        std::operator<<<std::char_traits<char> > (13F2F12C0h)
000000013F2F11EF  mov         rcx,rax
000000013F2F11F2  movss       xmm1,dword ptr [rsp+58h]
000000013F2F11F8  call        qword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (13F2F20A0h)]
000000013F2F11FE  lea         rdx,[__xi_z+64h (13F2F2234h)]
000000013F2F1205  mov         rcx,rax
000000013F2F1208  call        std::operator<<<std::char_traits<char> > (13F2F12C0h)
000000013F2F120D  mov         rcx,rax
000000013F2F1210  movss       xmm1,dword ptr [rsp+5Ch]
000000013F2F1216  call        qword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (13F2F20A0h)]
000000013F2F121C  mov         rdx,qword ptr [__imp_std::endl (13F2F2088h)]
000000013F2F1223  mov         rcx,rax
000000013F2F1226  call        qword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (13F2F2098h)]
    system("PAUSE");
000000013F2F122C  lea         rcx,[__xi_z+70h (13F2F2240h)]
000000013F2F1233  call        qword ptr [__imp_system (13F2F2180h)]
    return  0;
000000013F2F1239  xor         eax,eax
}

最佳答案

您可以这样写:

    mat4 Rm ={1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1};

cppreference.com

关于c++ - 在 Release模式下c++不正确地初始化4x4矩阵,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25729038/

10-13 07:09