몇 가지 수치 계산을 위해 함수 포인터를 사용하는 작은 프로그램을 작성했습니다."free() : 다음 크기가 유효하지 않음"으로 C++ 코드가 충돌 함
double polynom(const int j, const double xi) {
return pow(xi, j);
}
/**
* Calculate the legendre_polynom l_end on a certain position xi.
*/
double legendre_polynom(const int l_end, const double xi) {
vector <double> p_l(l_end+1);
p_l[0] = 1.0;
p_l[1] = xi;
for (int x = 2; x <= l_end; x++) {
// p_l = ((2x-1) * p_{x-1} - (x-1) * p_{x-2})/l
p_l[x] = ((2 * x - 1) * p_l[x - 1] - (x - 1) * p_l[x - 2])/x;
}
double result = p_l[l_end];
return result;
}
비정상적인 free() 오류로 프로그램이 중단됩니다. 첫 번째 함수 (다항식) 함수 포인터를 변경하면 제대로 작동하지만 legendre_polynom과 함께 실패합니다.
나는 그 기능을 빠져 나간 직후와 다른 코드가 계속되기 전에 이미 디버깅을했다.
*** glibc detected *** blub: free(): invalid next size (fast): 0x0804f248 ***
======= Backtrace: ========= /lib/i386-linux-gnu/libc.so.6(+0x6ebc2)[0xb7d70bc2]
/lib/i386-linux-gnu/libc.so.6(+0x6f862)[0xb7d71862]
/lib/i386-linux-gnu/libc.so.6(cfree+0x6d)[0xb7d7494d]
...
number2(_ZN9__gnu_cxx13new_allocatorIdE10deallocateEPdj+0x11)[0x804bc8b]
number2(_ZNSt12_Vector_baseIdSaIdEE13_M_deallocateEPdj+0x25)[0x804bbc3]
number2(_ZNSt12_Vector_baseIdSaIdEED1Ev+0x37)[0x804ba33]
number2(_ZNSt6vectorIdSaIdEED1Ev+0x38)[0x804b8a0]
number2(_Z16legendre_polynomid+0x13f)[0x804af9b]
그래서 제 질문은 여기에 잘못된 무엇인가?
문제를 나타내는 완벽한 최소한의 예를 제공 할 수 있습니까? –
또한 'l'은 가능한 가장 나쁜 변수 이름입니다! –
아마도 함수 포인터를 사용하는 코드를 게시해야합니다 ... – stefan