Qt程序崩溃-O2优化

时间:2015-10-09 21:56:14

标签: c++ qt

我正在使用带有MSVC2013 64位编译器的Qt创建器。当我使用-O2标志编译并运行程序时,它会给出一个错误:读取访问冲突:0x0,flags = 0x0(第一次机会)。使用带有-O2标志的调试器我发现xcomplex中发生了访问冲突。但是我在堆栈中创建所有变量。当我使用带有-O2的cl.exe直接编译程序时,没有错误。那么,我的问题是什么造成了这个错误?这是main.cpp:

#include <complex>
#include <cmath>
#include <vector>
#include <iostream>

#define M_PI       3.14159265358979323846

int main()
{
double f = 30;
double d = 0.5;
double n=3;
double k = 0.01;
using dcomplex = std::complex<double>;
static const dcomplex j = dcomplex(0,1);
dcomplex N = n+j*k;
dcomplex t = 5.0*N/std::pow(1.0+N,2);
dcomplex r = 2.0-t;
dcomplex delta = 2.0*M_PI*N*f*d/10.0;
dcomplex eid = std::exp(j*delta);
dcomplex eid2 = std::exp(2.0*j*delta);
dcomplex reid2 = 1.0-r*eid2;
dcomplex P = t/reid2;
dcomplex T = P*eid;
double absT = std::abs(T);
double angleT = 2*M_PI*(n-1)*f*d/10+std::arg(P);
dcomplex t_n = 4.0*(1.0-N)/std::pow(1.0+N,3);
dcomplex t_k = j*t_n;
dcomplex r_n = -t_n;
dcomplex r_k = -t_k;
double delta_n = 2*M_PI*f*d/10;
dcomplex delta_k = j*delta_n;
dcomplex dnr = j*delta_n*r;
dcomplex dkr = j*delta_k*r;
dcomplex P_n = (t_n+P*eid2*(r_n+2.0*dnr))/reid2;
dcomplex P_k = (t_k+P*eid2*(r_k+2.0*dkr))/reid2;

dcomplex T_n = (j*delta_n*P+P_n)*eid;
dcomplex T_k = (j*delta_k*P+P_k)*eid;

double absT_n = std::real(T_n*std::conj(T))/absT;
double absT_k = std::real(T_k*std::conj(T))/absT;

dcomplex t_nn=8.0*(N-2.0)/std::pow(1.0+N,4);
dcomplex r_nn = -t_nn;
dcomplex t_nk = j*t_nn;
dcomplex r_nk = -t_nk;
dcomplex t_kk = -t_nn;
dcomplex r_kk = -t_kk;

dcomplex A_n = eid2*(4.0*j*P*delta_n*r_n+P_n*r_n+2.0*dnr*P_n+P*r_nn-4.0*std::pow(delta_n,2)*P*r);
dcomplex A_k = eid2*(P_k*r_n+2.0*P_k*dnr+2.0*j*delta_k*P*r_n-4.0*delta_n*delta_k*r*P+P*r_nk+2.0*j*P*delta_n*r_k);

dcomplex P_nn = (t_nn+A_n+P_n*(r_n+2.0*dnr)*eid2)/reid2;
dcomplex A_k1 = eid2*(P_k*r_k+P_k*dkr*2.0+j*delta_k*P*r_k*2.0-delta_k*delta_k*r*P*4.0+P*r_kk+j*P*delta_k*r_k*2.0);

dcomplex P_nk = (t_nk+A_k+P_n*eid2*(r_k+2.0*dkr))/reid2;
dcomplex P_kk = (t_kk+A_k1+P_k*(r_k+2.0*dkr)*eid2)/reid2;

double a = std::real(P);
double b = std::imag(P);
double a_n = std::real(P_n);
double a_k = std::real(P_k);
double b_n = std::imag(P_n);
double b_k = std::imag(P_k);

double ab = std::pow(a,2)+std::pow(b,2);
double angleT_n = 2*M_PI*f*d/10-b/ab*a_n+a/ab*b_n;
double angleT_k = -b/ab*a_k+a/ab*b_k;

double a_nn = std::real(P_nn);
double a_kk = std::real(P_kk);
double a_nk = std::real(P_nk);
double b_nn = std::imag(P_nn);
double b_kk = std::imag(P_kk);
double b_nk = std::imag(P_nk);

double angleT_nn = (a*b_nn-b*a_nn-(angleT_n-delta_n)*2.0*(a*a_n+b*b_n))/ab;//access violation here
double angleT_nk = (a_k*b_n+a*b_nk-b_k*a_n-b*a_nk-2.0*(angleT_n-delta_n)*(a*a_k+b*b_k))/ab;
double angleT_kk = (a*b_kk-b*a_kk-2.0*angleT_k*(a*a_k+b*b_k))/ab;

dcomplex T_nn = eid*(-std::pow(delta_n,2)*P+j*delta_n*P_n+P_nn+j*delta_n*P_n);
dcomplex T_kk = eid*(-std::pow(delta_k,2)*P+j*delta_k*P_k+P_kk+j*delta_k*P_k);
dcomplex t1 = -delta_n*delta_k*P+j*delta_n*P_k+P_nk+j*delta_k*P_n;
dcomplex T_nk = eid*t1;

double absT_nn = (std::real(T_nn*std::conj(T))+std::pow(abs(T_n),2)-std::pow(absT_n,2))/absT;
double absT_kk = (std::real(T_kk*std::conj(T))+std::pow(abs(T_k),2)-std::pow(absT_k,2))/absT;
double absT_nk = (std::real(T_nk*std::conj(T)+std::conj(T_k)*T_n)-absT_n*absT_k)/absT;

std::vector<double> result = {absT, angleT, absT_n, absT_k, angleT_n, angleT_k, absT_nn, absT_nk, absT_kk, angleT_nn, angleT_nk, angleT_kk};
for(int i=0;i<result.size();i++){
    std::cout<<result[i]<<'\n';
}
return 0;
}

0 个答案:

没有答案