P
Pallav singh
Hi All ,
i am allocating all the STL dataStructre on Stack .... but Facing
double free or corruption (out).
Kindly Help me .
[root@arnav Dynamic_Programming]# ./a.out
Value of Rod Cut 0 with First OptimalCut at 0 Total Cost 0
Value of Rod Cut 1 with First OptimalCut at 1 Total Cost 1
Value of Rod Cut 2 with First OptimalCut at 2 Total Cost 5
Value of Rod Cut 3 with First OptimalCut at 3 Total Cost 8
Value of Rod Cut 4 with First OptimalCut at 2 Total Cost 10
Value of Rod Cut 5 with First OptimalCut at 2 Total Cost 13
Value of Rod Cut 6 with First OptimalCut at 6 Total Cost 17
Value of Rod Cut 7 with First OptimalCut at 1 Total Cost 18
Value of Rod Cut 8 with First OptimalCut at 2 Total Cost 22
*** glibc detected *** ./a.out: double free or corruption (out):
0x0817a0d8 ***
*** glibc detected *** ./a.out: free(): invalid size: 0x0817a100 ***
//////////////////////////////////////////////////////////////////////////////////////////////////////////
[root@arnav Dynamic_Programming]# cat RodCut.cc
# include <stdio.h>
# include <limits.h>
# include <iostream>
# include <vector>
using namespace std;
// Find the Best way of Rod Cut of Length N.
// Assume each Length of Rod has a price Pi.
void
OptimalValue (vector <unsigned int> cost, unsigned int length)
{
unsigned int i, j;
unsigned int min;
// Here we store result of OverLapping Problems
// which we can use as Computation Grows
vector <unsigned int> Optimal_Value (length, 0);
vector <unsigned int> First_Optimal_Cut (length, 0);
Optimal_Value[0] = 0;
First_Optimal_Cut[0] = 0;
for (i = 1; i <= length; i++)
{
min = 0;
for (j = 1; j <= i ; j++)
if (min < (cost[j] + Optimal_Value[i - j]))
{
min = cost[j] + Optimal_Value[i - j];
First_Optimal_Cut = j;
}
Optimal_Value = min;
}
for (i = 0 ; i < length ; i++)
{
unsigned int price = 0;
unsigned int k = i;
while(k > 0)
{
price = price + cost[First_Optimal_Cut[k]];
k = k - First_Optimal_Cut[k];
}
cout << " Value of Rod Cut " << i << " with First
OptimalCut at " << First_Optimal_Cut
<< " Total Cost " << price << endl;
}
}
int main()
{
// length i = 0 1 2 3 4 5 6 7 8
// price pi = 0 1 5 8 9 10 17 17 20
// i 0 1 2 3 4 5 6 7 8
// ri 0 1 5 8 10 13 17 18 22
// si 0 1 2 3 2 2 6 1 2
vector<unsigned int > price;
price.push_back(0);
price.push_back(1);
price.push_back(5);
price.push_back(8);
price.push_back(9);
price.push_back(10);
price.push_back(17);
price.push_back(17);
price.push_back(20);
OptimalValue (price, price.size());
return 0;
}
Thanks
Pallav Singh
i am allocating all the STL dataStructre on Stack .... but Facing
double free or corruption (out).
Kindly Help me .
[root@arnav Dynamic_Programming]# ./a.out
Value of Rod Cut 0 with First OptimalCut at 0 Total Cost 0
Value of Rod Cut 1 with First OptimalCut at 1 Total Cost 1
Value of Rod Cut 2 with First OptimalCut at 2 Total Cost 5
Value of Rod Cut 3 with First OptimalCut at 3 Total Cost 8
Value of Rod Cut 4 with First OptimalCut at 2 Total Cost 10
Value of Rod Cut 5 with First OptimalCut at 2 Total Cost 13
Value of Rod Cut 6 with First OptimalCut at 6 Total Cost 17
Value of Rod Cut 7 with First OptimalCut at 1 Total Cost 18
Value of Rod Cut 8 with First OptimalCut at 2 Total Cost 22
*** glibc detected *** ./a.out: double free or corruption (out):
0x0817a0d8 ***
*** glibc detected *** ./a.out: free(): invalid size: 0x0817a100 ***
//////////////////////////////////////////////////////////////////////////////////////////////////////////
[root@arnav Dynamic_Programming]# cat RodCut.cc
# include <stdio.h>
# include <limits.h>
# include <iostream>
# include <vector>
using namespace std;
// Find the Best way of Rod Cut of Length N.
// Assume each Length of Rod has a price Pi.
void
OptimalValue (vector <unsigned int> cost, unsigned int length)
{
unsigned int i, j;
unsigned int min;
// Here we store result of OverLapping Problems
// which we can use as Computation Grows
vector <unsigned int> Optimal_Value (length, 0);
vector <unsigned int> First_Optimal_Cut (length, 0);
Optimal_Value[0] = 0;
First_Optimal_Cut[0] = 0;
for (i = 1; i <= length; i++)
{
min = 0;
for (j = 1; j <= i ; j++)
if (min < (cost[j] + Optimal_Value[i - j]))
{
min = cost[j] + Optimal_Value[i - j];
First_Optimal_Cut = j;
}
Optimal_Value = min;
}
for (i = 0 ; i < length ; i++)
{
unsigned int price = 0;
unsigned int k = i;
while(k > 0)
{
price = price + cost[First_Optimal_Cut[k]];
k = k - First_Optimal_Cut[k];
}
cout << " Value of Rod Cut " << i << " with First
OptimalCut at " << First_Optimal_Cut
<< " Total Cost " << price << endl;
}
}
int main()
{
// length i = 0 1 2 3 4 5 6 7 8
// price pi = 0 1 5 8 9 10 17 17 20
// i 0 1 2 3 4 5 6 7 8
// ri 0 1 5 8 10 13 17 18 22
// si 0 1 2 3 2 2 6 1 2
vector<unsigned int > price;
price.push_back(0);
price.push_back(1);
price.push_back(5);
price.push_back(8);
price.push_back(9);
price.push_back(10);
price.push_back(17);
price.push_back(17);
price.push_back(20);
OptimalValue (price, price.size());
return 0;
}
Thanks
Pallav Singh