I will simplify the problem in order to present a minimal and reproducible example:
I have a class Polynomial
which is just a wrapper around an array of coefficients. For convenience in my analyses downstream it is a good idea to write it as a template regarding to the max. degree it can represent. Please remember that an N
-th degree polynomial will hold N+1
coefficients.
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];
}
};
In my main
I test the code with creating one polynomial and then a one-sized array for it:
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
I get the following memory 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
In reality my code is much more complex but I commented out as much as I could and I really can't see where does the error come from(?)