Please give me a hint!

Discussion in 'C++' started by chuan, Apr 23, 2009.

  1. chuan

    chuan Guest

    I am trying to write a class for jackknife algorithm.
    I need a dynamic array, which I call jack, inside the class.
    Here is my code in jackknife.h

    #include <iostream>
    #include <vector>
    #include <math.h>

    class jackknife{
    int bin;
    double* jack;
    double* data;

    public:
    jackknife();
    ~jackknife();
    int setbin(int);
    };

    jackknife::jackknife() {
    bin = 0;
    }

    jackknife::~jackknife(){
    delete [] jack;
    }

    int jackknife::setbin(int n) {
    bin = n;
    jack = new double [n];
    return bin;
    }

    I allocat memory in setbin, then debugger tells me
    the program crashes in the deconstructor, when I free the memory.

    I have little experience of C++, I realize the memory management must
    be wrong.
    But how to correct it.

    Excuse me for my bad english.
    chuan, Apr 23, 2009
    #1
    1. Advertising

  2. chuan

    chuan Guest

    On Apr 23, 12:10 am, Paavo Helde <> wrote:
    > Paavo Helde <> kirjutas:
    >
    >
    >
    >
    >
    > > chuan <> kirjutas:

    >
    > >> I am trying to write a class for jackknife algorithm.
    > >> I need a dynamic array, which I call jack, inside the class.
    > >> Here is my code in jackknife.h

    >
    > >> #include <iostream>
    > >> #include <vector>
    > >> #include <math.h>

    >
    > >> class jackknife{
    > >>   int bin;
    > >>   double* jack;
    > >>   double* data;

    > [...]
    >
    > >> int jackknife::setbin(int n) {
    > >>   bin = n;
    > >>   jack = new double [n];

    >
    > Coming to think about that, any particular reason to not use std::vector
    > <double> instead of raw pointers? You have already #included <vector>...
    >
    > Paavo


    Yes, I want to use std::vector, but I got the same error at
    deconstructor.
    Your first post give me the right direction. I once made the same
    mistake, now again.
    Thank you very much.
    chuan, Apr 23, 2009
    #2
    1. Advertising

  3. chuan wrote:
    > Yes, I want to use std::vector, but I got the same error at
    > deconstructor.


    So rather than figuring out the problem instead you want to make your
    class unsafer by allocating the array manually?

    > Your first post give me the right direction.


    Your class still has tons of problems (such as setbin() leaking memory).
    Juha Nieminen, Apr 23, 2009
    #3
  4. chuan

    mzdude Guest

    On Apr 22, 10:51 pm, chuan <> wrote:
    > I am trying to write a class for jackknife algorithm.
    > I need a dynamic array, which I call jack, inside the class.
    > Here is my code in jackknife.h
    >
    > #include <iostream>
    > #include <vector>
    > #include <math.h>
    >
    > class jackknife{
    >   int bin;
    >   double* jack;
    >   double* data;
    >
    > public:
    >   jackknife();
    >   ~jackknife();
    >   int setbin(int);
    >
    > };
    >
    > jackknife::jackknife() {
    >   bin = 0;
    >
    > }
    >
    > jackknife::~jackknife(){
    >     delete [] jack;
    >
    > }
    >
    > int jackknife::setbin(int n) {
    >   bin = n;
    >   jack = new double [n];
    >   return bin;
    >
    > }
    >
    > I allocat memory in setbin, then debugger tells me
    > the program crashes in the deconstructor, when I free the memory.
    >
    > I have little experience of C++, I realize the memory management must
    > be wrong.
    > But how to correct it.


    class jackknife {
    std::vector<double> jack;
    std::vector<double> data;

    size_t setbin( size_t newSize )
    {
    jack.resize(newSize);
    return newSize;
    }

    };

    Note that you no longer need to worry about constructors, destructors
    or copy operators.
    mzdude, Apr 23, 2009
    #4
  5. From what I read, it seems all the code above is inside a header file?
    Non-inline function declarations should be in a separate .c or .cpp
    file.

    If you are still considering using the more C-ish approach with
    pointers to the instead of using a vector, if you look closely at
    setbin() you will realize you are not freeing the memory from previous
    call before allocating more.

    And, more information like the detailed error message would be great.

    Best regards,
    Victor Freire



    On Apr 23, 9:51 am, mzdude <> wrote:
    > On Apr 22, 10:51 pm, chuan <> wrote:
    >
    >
    >
    >
    >
    > > I am trying to write a class for jackknife algorithm.
    > > I need a dynamic array, which I call jack, inside the class.
    > > Here is my code in jackknife.h

    >
    > > #include <iostream>
    > > #include <vector>
    > > #include <math.h>

    >
    > > class jackknife{
    > >   int bin;
    > >   double* jack;
    > >   double* data;

    >
    > > public:
    > >   jackknife();
    > >   ~jackknife();
    > >   int setbin(int);

    >
    > > };

    >
    > > jackknife::jackknife() {
    > >   bin = 0;

    >
    > > }

    >
    > > jackknife::~jackknife(){
    > >     delete [] jack;

    >
    > > }

    >
    > > int jackknife::setbin(int n) {
    > >   bin = n;
    > >   jack = new double [n];
    > >   return bin;

    >
    > > }

    >
    > > I allocat memory in setbin, then debugger tells me
    > > the program crashes in the deconstructor, when I free the memory.

    >
    > > I have little experience of C++, I realize the memory management must
    > > be wrong.
    > > But how to correct it.

    >
    > class jackknife {
    >   std::vector<double> jack;
    >   std::vector<double> data;
    >
    > size_t setbin( size_t newSize )
    >   {
    >      jack.resize(newSize);
    >      return newSize;
    >   }
    >
    > };
    >
    > Note that you no longer need to worry about constructors, destructors
    > or copy operators.- Hide quoted text -
    >
    > - Show quoted text -
    Victor Freire, Apr 24, 2009
    #5
    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. Daniel Pope
    Replies:
    5
    Views:
    457
    Silvio Bierman
    Feb 22, 2004
  2. Porky Pig Jr
    Replies:
    3
    Views:
    369
    Dave Cole
    Aug 24, 2004
  3. Robert Brewer
    Replies:
    4
    Views:
    380
    Porky Pig Jr
    Aug 24, 2004
  4. Garg
    Replies:
    4
    Views:
    930
  5. grocery_stocker
    Replies:
    10
    Views:
    610
    Keith Thompson
    May 25, 2005
Loading...

Share This Page