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

Discussion in 'C++' started by Pallav singh, Jan 29, 2012.

  1. Pallav singh

    Pallav singh Guest

    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
     
    Pallav singh, Jan 29, 2012
    #1
    1. Advertising

  2. Pallav singh <> 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 = 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());


    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 ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Jan 29, 2012
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Grant Edwards

    glibc detected double free or corruption

    Grant Edwards, Dec 20, 2006, in forum: Python
    Replies:
    0
    Views:
    429
    Grant Edwards
    Dec 20, 2006
  2. Delaney, Timothy (Tim)

    RE: glibc detected double free or corruption

    Delaney, Timothy (Tim), Dec 20, 2006, in forum: Python
    Replies:
    0
    Views:
    528
    Delaney, Timothy (Tim)
    Dec 20, 2006
  3. chuan
    Replies:
    2
    Views:
    3,101
    James Kanze
    Nov 15, 2008
  4. Gabriel Genellina
    Replies:
    1
    Views:
    574
    Gabriel Genellina
    Feb 25, 2009
  5. Aryeh Leib Taurog

    glibc detected double free or corruption

    Aryeh Leib Taurog, Feb 8, 2010, in forum: Python
    Replies:
    0
    Views:
    1,100
    Aryeh Leib Taurog
    Feb 8, 2010
Loading...

Share This Page