I was working to implement the rod cutting problem and I encountered this error:
a.out: malloc.c:2392: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
Aborted (core dumped)
The dashed comment block is where I suspect the error is coming from, but I'm not sure why. Trying to run under gdb I am having hard time pin-pointing it (seems to be crashing at i = 7) (compiling using: g++)
#include <iostream>
#include <cstdlib>
using namespace std;
int max_from(int * arr, int n)
{
int maxval = arr[1];
for(size_t i = 2; i < n; ++i)
if(arr[i] > maxval) maxval = arr[i];
return maxval;
}
int main()
{
int n = 11;
int * V = new int[n]; // given arr, price at(i)
int * C = new int[n]; // optimal at length(i)
V[0] = 0; // non-affecting value, using 1 as starting point
V[1] = 1;
V[2] = 5;
V[3] = 8;
V[4] = 9;
V[5] = 10;
V[6] = 17;
V[7] = 17;
V[8] = 20;
V[9] = 24;
V[10] = 30;
C[0] = 0;
// --------------------------------------------------
for(size_t i = 1; i < n; ++i)
{
int * arr = new int[i];
for(size_t k = 1; k <= i; ++k)
{
arr[k] = V[k] + C[i-k];
}
C[i] = max_from(arr, n);
delete [ ] arr;
}
cout << max_from(C, n) << '\n'; // maximized cost
// --------------------------------------------------
delete [ ] V;
delete [ ] C;
}
Aucun commentaire:
Enregistrer un commentaire