Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #13645 > unrolled thread
| Started by | Pallav singh <singh.pallav@gmail.com> |
|---|---|
| First post | 2012-01-29 02:25 -0800 |
| Last post | 2012-01-29 12:15 +0000 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.c++
Error ::: *** glibc detected *** ./a.out: double free or corruption (out): Pallav singh <singh.pallav@gmail.com> - 2012-01-29 02:25 -0800
Re: Error ::: *** glibc detected *** ./a.out: double free or corruption (out): Paavo Helde <myfirstname@osa.pri.ee> - 2012-01-29 05:56 -0600
Re: Error ::: *** glibc detected *** ./a.out: double free or corruption (out): jt@toerring.de (Jens Thoms Toerring) - 2012-01-29 12:15 +0000
| From | Pallav singh <singh.pallav@gmail.com> |
|---|---|
| Date | 2012-01-29 02:25 -0800 |
| Subject | Error ::: *** glibc detected *** ./a.out: double free or corruption (out): |
| Message-ID | <e2f17379-c307-4b33-bfe4-12d1f9867343@i10g2000pbl.googlegroups.com> |
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[i] = j;
}
Optimal_Value[i] = 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[i]
<< " 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
[toc] | [next] | [standalone]
| From | Paavo Helde <myfirstname@osa.pri.ee> |
|---|---|
| Date | 2012-01-29 05:56 -0600 |
| Message-ID | <Xns9FE98DC9024myfirstnameosapriee@216.196.109.131> |
| In reply to | #13645 |
Pallav singh <singh.pallav@gmail.com> wrote in
news:e2f17379-c307-4b33-bfe4-12d1f9867343@i10g2000pbl.googlegroups.com:
> void
> OptimalValue (vector <unsigned int> cost, unsigned int length)
[...]
>
> for (i = 1; i <= length; i++)
> {
> min = 0;
> for (j = 1; j <= i ; j++)
> if (min < (cost[j] + Optimal_Value[i - j]))
In C++, array indexes start from zero, not from 1. This means accessing
cost[length] is illegal, for example. You probably wanted to write
something like
for (unsigned int i = 0; i < length; i++)
and likewise for j.
hth
Paavo
[toc] | [prev] | [next] | [standalone]
| From | jt@toerring.de (Jens Thoms Toerring) |
|---|---|
| Date | 2012-01-29 12:15 +0000 |
| Message-ID | <9okrj8Fe93U1@mid.uni-berlin.de> |
| In reply to | #13645 |
Pallav singh <singh.pallav@gmail.com> wrote:
> 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[i] = j;
> }
> Optimal_Value[i] = 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[i]
> << " 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());
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
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web