我将简化问题以提供一个最小且可重现的示例:
我有一个类Polynomial
,它只是一个系数数组的包装器。为了方便我的下游分析,最好将其编写为关于最大值的模板。可以代表的程度。请记住,N
第-次多项式将保存N+1
系数。
template <const unsigned int MaxDeg>
class Polynomial {
public:
int *coefficients;
Polynomial(int* arr) {
coefficients = new int[MaxDeg+1];
for (unsigned int i=0; i <= MaxDeg; i++) coefficients[i] = arr[i];
}
Polynomial() {
coefficients = new int[MaxDeg+1]();
}
~Polynomial() {
delete[] coefficients;
}
int& operator[](int index) const {
return coefficients[index];
}
};
在我main
的测试代码中,我创建了一个多项式,然后为它创建了一个大小的数组:
int f_q[] = {5, 9, 6, 16, 4, 15, 16, 22, 20, 18, 30};
Polynomial<10> P_f_q(f_q);
Polynomial<10> P_arr[1] = {P_f_q}; // this line raises error
我收到以下内存错误:
test(33770,0x10de6be00) malloc: *** error for object 0x7fef43605420: pointer being freed was not allocated
test(33770,0x10de6be00) malloc: *** set a breakpoint in malloc_error_break to debug
[1] 33770 abort ./test
实际上我的代码要复杂得多,但我尽可能多地注释掉,我真的看不出错误来自哪里(?)