Windowsでは出るエラーでMacでは出ない
現在、Visual C++ 2013 Expressを使っているのですが、そちらではエラーが出るのに、MacbookのXcodeで走らせたらエラーも出ずに正常に実行することができます。
いったい何が原因でエラーが起こっているのでしょうか?
エラーメッセージは"XXXXX.exe has triggered a breakpoint."で"malloc.c"のタブがVC++上に開きます。以下がそのコードになるのですが、アドバイス宜しくお願い致します。
#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <complex>
#include <stdlib.h>
using namespace std;
typedef complex<double> dcmplx;
complex<double> M_i = dcmplx(0, 1);
//---------------------------------------------------------------
// Characteristic Function
dcmplx* CharFunc(double alpha, double* nu, double s0, double r, double q, double sigma, double t, int N)
{
dcmplx* out = (dcmplx*)malloc(sizeof(dcmplx) * N);
dcmplx* u = (dcmplx*)malloc(sizeof(dcmplx) * N);
for (int j = 0; j < N; ++j){ // j = 1,...,N
u[j] = nu[j] - (alpha + 1) * M_i;
out[j] = exp(M_i*(log(s0) + (r - q - sigma*sigma / 2.0)*t)*u[j] - sigma*sigma * u[j] * u[j] * t / 2.0);
}
return out;
}
// Characteristic Function for C
dcmplx CharFuncC(double u, double s0, double K, double r, double q, double sigma, double t, int N)
{
return exp(M_i*((log(s0) - log(K)) + (r - q - sigma*sigma / 2.0)*t)*u - sigma*sigma * u*u * t / 2.0);
}
//---------------------------------------------------------------
// Main
int main(int, char**) {
double alpha[] = { -1.2, -1.5, -5.0, -10.0 };
double eta;
double s0;
double r;
double q;
double sigma;
double t;
double K;
int pow2;
int N;
int m_atm = 0;
FILE *fp;
if (!(fp = fopen("inputa.txt", "r"))){
printf("File \'inputa\' could not be opened!");
exit(-1);
}
fscanf(fp, "%lg%lg%lg%lg%lg%lg%d", &eta, &s0, &r, &q, &sigma, &t, &pow2);
double a;
double b;
double v;
double abs_ab[] = { 2.0, 5.0, 10.0, 20.0 };
double sum =0;
for (pow2 = 7; pow2 <= 10; ++pow2){
N = (int)pow(2.0, pow2);
double *nu = (double*)malloc(sizeof(double)* N);// nu * (b - a) / M_PI;
double *Vp = (double*)malloc(sizeof(double)* N);
double *chi = (double*)malloc(sizeof(double)* N);
double *phi = (double*)malloc(sizeof(double)* N);
dcmplx *Phi = (dcmplx*)malloc(sizeof(double)* N);
for (int i = 0; i < 4; ++i){
b = abs_ab[i];
a = -b;
cout << "N:" << N << ", (a, b): (" << a << ", " << b << ")" << endl;
for (K = 1600; K <= 1900; K += 100){
for (int k = 0; k < N; ++k){
nu[k] = k * M_PI / (b - a);
// chi for Put with (c,d) = (a,0)
chi[k] = 1.0 / (1.0 + nu[k] * nu[k]) * (cos(nu[k] * (-a)) - exp(a) + nu[k] * sin(nu[k] * (-a)));
// phi for Put with (c,d) = (a,0)
phi[k] = sin(nu[k] * (-a)) * (1.0 / nu[k]);
phi[0] = -a;
// COS expasion coeff V for Put
Vp[k] = (2.0 / (b - a)) * K * (-chi[k] + phi[k]);
// Characteristic Function for C, (just different argument inputs)
nu[k] = k * M_PI / (b - a);
Phi[k] = CharFuncC(nu[k], s0, K, r, q, sigma, t, N);
sum += real(Phi[k] * exp(-M_i*(k*M_PI) * a / (b - a)))*Vp[k];
if (k == 0) sum /= 2.0;
}
v = exp(-r*t)*sum;
cout << "strike=" << K << ", price=" << v << endl;
sum = 0.0;
}
}
}
system("Pause");
return 0;
}
お礼
補足
丁寧な返答ありがとうございます。コンストラクタに関しより詳しい情報を探しているところです