HEAP error when trying to use free() help needed

N

none

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
 
F

Fred Zwarts \(KVI\)

"none" wrote in message
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++".
 
B

Balog Pal

none said:
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"
 
J

Juha Nieminen

io_x said:
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.)
 
M

Mustansir

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.
 
N

none

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
 
I

Ian Collins

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?
 
N

none

Two things:

why are you using malloc?

sizeof(char) is one by definition.

















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

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.
 
J

Juha Nieminen

none said:
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).
 
N

none

  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.
 
I

Ian Collins

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?
 
J

Juha Nieminen

none said:
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.
 

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

No members online now.

Forum statistics

Threads
473,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top