# 数据结构 有关多维数组的问题

int main(){
nArray A(2, 3, 4);
}
/******************************/
template
class nArray{
private:
aElem* base;
int dim;
int * bounds;
int * constants;
private:
Status Locate(va_list, int&);
public:
nArray(int, ...);
~nArray(){ if (base) delete[] base; if (bounds) delete[]base; if (constants) delete[]constants; }
Status Value(aElem&, ...);
Status Assign(aElem, ...);
};
template
nArray::nArray(int d, ...) :dim(d), base(nullptr), bounds(nullptr), constants(nullptr){
va_list ap;
va_start(ap, d);
int elemtotal = 1;
bounds = new int[dim];
constants = new int[dim];//运行到这行前，dim=2，貌似没问题，可是一到这行运行完，dim就变成了1323297024，并且有时程序崩溃出错，我实在找不到原因了，希望有前辈能指点下
for (int i = 0; i < dim; i++){
bounds[i] = va_arg(ap, int);
if (bounds[i] < 0) exit(UNDERFLOW);
elemtotal *= bounds[i];
}
va_end(ap);
base = new int[elemtotal];
if (!base) exit(OVERFLOW);
constants[dim - 1] = 1;
for (int i = dim - 2; i >= 0; --i)
constants[i] = constants[i + 1] * bounds[i + 1];
}

2015/08/28 10:01
• 数组
• 数据结构
• 点赞
• 收藏
• 回答
私信