HEAP error when trying to use free() help needed

Discussion in 'C++' started by none, Jun 7, 2011.

  1. none

    none Guest

    I'm trying to figure out why this testing code do not work and produce
    an error in VC++ when returning from the foo method.

    here's the code:

    class test {
    public:
    char* pPointer;

    test (void) {
    pPointer = (char *) malloc (sizeof(char));

    }

    ~test () {
    free (pPointer); // HEAP error! when returning from foo
    }

    test test::foo (void) {
    test myfoo;
    return myfoo;
    }
    };

    ...

    test atest;
    atest = atest.foo(); // HEAP error!

    The pPointer seems to be already freed by foo, why is this? How
    can I make this code work?

    Thanks in advance
     
    none, Jun 7, 2011
    #1
    1. Advertising

  2. "none" wrote in message
    news:...

    >I'm trying to figure out why this testing code do not work and produce
    >an error in VC++ when returning from the foo method.
    >
    >here's the code:
    >
    > class test {
    > public:
    > char* pPointer;
    >
    > test (void) {
    > pPointer = (char *) malloc (sizeof(char));
    >
    > }
    >
    > ~test () {
    > free (pPointer); // HEAP error! when returning from foo
    > }
    >
    > test test::foo (void) {
    > test myfoo;
    > return myfoo;
    > }
    > };
    >
    > ...
    >
    > test atest;
    > atest = atest.foo(); // HEAP error!
    >
    > The pPointer seems to be already freed by foo, why is this? How
    >can I make this code work?
    >
    > Thanks in advance


    Yes, since you are copying the test objects, the heap is freed twice.
    Google for "Rule of three in C++".
     
    Fred Zwarts \(KVI\), Jun 7, 2011
    #2
    1. Advertising

  3. none

    Balog Pal Guest

    "none" <>
    > class test {
    > public:
    > char* pPointer;
    >
    > test (void) {
    > pPointer = (char *) malloc (sizeof(char));
    >
    > }
    >
    > ~test () {
    > free (pPointer); // HEAP error! when returning from foo
    > }
    >


    Look up "c++ rule of 3"
     
    Balog Pal, Jun 7, 2011
    #3
  4. io_x <> wrote:
    > test& fooVoid(void)
    > {static test myfoo(255);
    > return myfoo;
    > }


    Your solution to the lack of a copy constructor is to make a local
    static object which you return? That will only *mask* the problem, not
    solve it.

    > test& operator=(const test& a)
    > {*cPointer=*a.cPointer;
    > return *this;
    > }


    That assignment operator is horribly broken. (Hint: It will only copy
    the first character of the array.)
     
    Juha Nieminen, Jun 8, 2011
    #4
  5. none

    Mustansir Guest

    On Jun 7, 3:16 pm, none <> wrote:
    > I'm trying to figure out why this testing code do not work and produce
    > an error in VC++ when returning from the foo method.
    >
    > here's the code:
    >
    >     class test {
    >     public:
    >         char* pPointer;
    >
    >         test (void) {
    >            pPointer = (char *) malloc (sizeof(char));
    >
    >         }
    >
    >         ~test () {
    >             free (pPointer); // HEAP error! when returning from foo
    >         }
    >
    >         test test::foo (void) {
    >             test myfoo;
    >             return myfoo;
    >         }
    >     };
    >
    >     ...
    >
    >     test atest;
    >     atest = atest.foo(); // HEAP error!
    >
    >     The pPointer seems to be already freed by foo, why is this? How
    > can I make this code work?
    >
    >     Thanks in advance


    You should write a copy constructor and copy assignment operator that
    performs "deep copy". Your compiler synthesized versions of these just
    perform "shallow copy" by copying the pointer rather than the contents
    pointed to.
     
    Mustansir, Jun 8, 2011
    #5
  6. none

    Krice Guest

    On 7 kesä, 13:16, none <> wrote:
    > How can I make this code work?


    Don't use malloc/free in C++. Use new & delete.
     
    Krice, Jun 8, 2011
    #6
  7. none

    none Guest

    Thanks, everyone... sorry it took so long to reply...

    Here's the code I came up with:

    class test {
    public:


    test (void) {
    pPointer = (char *) malloc (sizeof(char));

    }

    // Copy constructor
    test (const test& mytest) {

    pPointer = (char *) malloc (sizeof(char));
    *pPointer = *mytest.pPointer;

    }

    ~test () {
    free (pPointer);
    }

    test foo (void) {
    test myfoo;
    return myfoo;
    }

    // = operator overload
    test& operator= (const test& mytest) {

    *pPointer = *mytest.pPointer;

    return *this;

    }

    char* pPointer;
    };

    Is it correct? (if we suppose that for the purpose of this test
    pPointer will only point to a single char)

    Thanks again
     
    none, Jun 20, 2011
    #7
  8. none

    Ian Collins Guest

    On 06/20/11 05:06 PM, none wrote:
    > Thanks, everyone... sorry it took so long to reply...
    >
    > Here's the code I came up with:
    >
    > class test {
    > public:
    >
    >
    > test (void) {
    > pPointer = (char *) malloc (sizeof(char));


    Two things:

    why are you using malloc?

    sizeof(char) is one by definition.

    > }
    >
    > // Copy constructor
    > test (const test& mytest) {
    >
    > pPointer = (char *) malloc (sizeof(char));
    > *pPointer = *mytest.pPointer;
    >
    > }
    >
    > ~test () {
    > free (pPointer);
    > }
    >
    > test foo (void) {
    > test myfoo;
    > return myfoo;
    > }
    >
    > // = operator overload
    > test& operator= (const test& mytest) {
    >
    > *pPointer = *mytest.pPointer;
    >
    > return *this;
    >
    > }
    >
    > char* pPointer;
    > };
    >
    > Is it correct? (if we suppose that for the purpose of this test
    > pPointer will only point to a single char)


    Well it won't leak or double free, but what do you really want to do?

    --
    Ian Collins
     
    Ian Collins, Jun 20, 2011
    #8
  9. none

    none Guest

    On Jun 20, 12:17 am, Ian Collins <> wrote:
    > On 06/20/11 05:06 PM, none wrote:
    >
    > > Thanks, everyone... sorry it took so long to reply...

    >
    > > Here's the code I came up with:

    >
    > >      class test {
    > >      public:

    >
    > >          test (void) {
    > >              pPointer = (char *) malloc (sizeof(char));

    >
    > Two things:
    >
    > why are you using malloc?
    >
    > sizeof(char) is one by definition.
    >
    >
    >
    >
    >
    > >          }

    >
    > >          // Copy constructor
    > >          test (const test&  mytest) {

    >
    > >               pPointer = (char *) malloc (sizeof(char));
    > >              *pPointer = *mytest.pPointer;

    >
    > >          }

    >
    > >          ~test () {
    > >              free (pPointer);
    > >          }

    >
    > >          test foo (void) {
    > >              test myfoo;
    > >              return myfoo;
    > >          }

    >
    > >          // = operator overload
    > >          test&  operator= (const test&  mytest) {

    >
    > >              *pPointer = *mytest.pPointer;

    >
    > >              return *this;

    >
    > >          }

    >
    > >          char* pPointer;
    > >      };

    >
    > > Is it correct? (if we suppose that for the purpose of this test
    > > pPointer will only point to a single char)

    >
    > Well it won't leak or double free, but what do you really want to do?
    >
    > --
    > Ian Collins


    It's just a simplified test of a more complex code that didn't work,
    I'm using just one char for the purpose of this test, but the final
    code will handle a string.
     
    none, Jun 20, 2011
    #9
  10. none <> wrote:
    > It's just a simplified test of a more complex code that didn't work,
    > I'm using just one char for the purpose of this test, but the final
    > code will handle a string.


    Any reason you can't use std::string or std::vector<char>? Using them
    instead of allocating the string yourself will not only make your code
    safer, it will also make it a lot simpler (you won't need to write a
    copy constructor and assignment operator).
     
    Juha Nieminen, Jun 20, 2011
    #10
  11. none

    none Guest

    On Jun 20, 1:06 am, Juha Nieminen <> wrote:
    > none <> wrote:
    > > It's just a simplified test of a more complex code that didn't work,
    > > I'm using just one char for the purpose of this test, but the final
    > > code will handle a string.

    >
    >   Any reason you can't use std::string or std::vector<char>? Using them
    > instead of allocating the string yourself will not only make your code
    > safer, it will also make it a lot simpler (you won't need to write a
    > copy constructor and assignment operator).


    In fact, I made a mistake when I said that the data was a string. The
    string will be converted into BCD values, so the data pointer will
    contain a arbitrary sized BCD number.
     
    none, Jun 20, 2011
    #11
  12. none

    Ian Collins Guest

    On 06/20/11 07:35 PM, none wrote:
    > On Jun 20, 1:06 am, Juha Nieminen<> wrote:
    >> none<> wrote:
    >>> It's just a simplified test of a more complex code that didn't work,
    >>> I'm using just one char for the purpose of this test, but the final
    >>> code will handle a string.

    >>
    >> Any reason you can't use std::string or std::vector<char>? Using them
    >> instead of allocating the string yourself will not only make your code
    >> safer, it will also make it a lot simpler (you won't need to write a
    >> copy constructor and assignment operator).

    >
    > In fact, I made a mistake when I said that the data was a string. The
    > string will be converted into BCD values, so the data pointer will
    > contain a arbitrary sized BCD number.


    But why are you using malloc, or raw pointers?

    --
    Ian Collins
     
    Ian Collins, Jun 20, 2011
    #12
  13. none <> wrote:
    > In fact, I made a mistake when I said that the data was a string. The
    > string will be converted into BCD values, so the data pointer will
    > contain a arbitrary sized BCD number.


    That still doesn't explain why you can't use std::vector.
     
    Juha Nieminen, Jun 20, 2011
    #13
    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. Michal Slocinski

    Heap dump file size vs heap size

    Michal Slocinski, Mar 25, 2008, in forum: Java
    Replies:
    1
    Views:
    766
    GArlington
    Mar 25, 2008
  2. viki
    Replies:
    6
    Views:
    613
    Erik Wikström
    Jun 28, 2008
  3. george
    Replies:
    0
    Views:
    1,180
    george
    Aug 29, 2008
  4. Raymond Schanks
    Replies:
    0
    Views:
    575
    Raymond Schanks
    Apr 11, 2010
  5. mohammed_a_o
    Replies:
    0
    Views:
    310
    mohammed_a_o
    Nov 30, 2010
Loading...

Share This Page