请教一个C++问题


所有跟贴·加跟贴·新语丝读书论坛

送交者: qtl 于 2010-09-09, 04:34:00:

写了一个非常简单的vector class通过MKL调用level 1 BLAS:
#include<mkl.h>
#include<stdexcept>

#ifndef _x_matrix
#define _x_matrix
class xVec{
private:
double *v;
int n;
public:
xVec():n(0),v(0){}
xVec(int a):n(a),v(new double[n]){}
xVec(int a, double c){
n=a;
v=new double[n];
for(int i=0; i<n; ++i) v=c;
}

~xVec(){if(v) delete[]v;}

double &operator()(int i){
if(i<n) return v;
else throw runtime_error("Out of boundary.");
}

double asum(int inc=1){
// return sum v by inc
return dasum(&n, v, &inc);
}
};
然后:
const int n(100);
xVec x(n,10););
cout<<x.asum()<<endl;

奇怪的是结果不稳定,有时结果对,有时错,有时比如出这样的错:
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
有时候是segment fault 等等。
====
请教哪里出问题了?




所有跟贴:


加跟贴

笔名: 密码: 注册笔名请按这里

标题:

内容: (BBCode使用说明