Error ::: *** glibc detected *** ./a.out: double free or corruption (out):

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
 
J

Jens Thoms Toerring

Pallav singh said:
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;

It's normally frowned upon to pull in all of the 'std' name-
space. Better declare what exactly you need from it. But that
hasn't any bearing on your problem here.
// 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;

I would define them only when they are actually needed.
// 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;

Both arrays are already initialized (for all elements) to 0,
so this is redundant.
for (i = 1; i <= length; i++)

'length' is the length of all the arrays you have here, so the
maximum index into these arrays you can use is 'length - 1'.
By having 'i' run up the value of 'length' you access and
assign to elements past the end of some of them. This can
easily result in all kinds of strange errors since you have
written to memory you shouldn't have and may have overwrit-
ten important information. As long a you're not 100% sure
that all array indices are ok you can use the at() function
instead of the [] operator - the program will then throw an
out of range exception if you made a mistake.
{
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;

OptimalValue (price, price.size());

Why do you pass the size? The 'price' vector knows all by
itself how large it is - this isn't a simple array.

Regards, Jens
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top