* Catching memory errors
@ 2005-05-22 2:06 Bala B K
2005-05-22 4:02 ` corey taylor
0 siblings, 1 reply; 2+ messages in thread
From: Bala B K @ 2005-05-22 2:06 UTC (permalink / raw)
To: gcc-help
Hi,
Recently I experienced a very strange behaviour with C++ (g++ 4.0.0,
g++ 3.4.1 and g++ 3.3.3) when I made a mistake and deleted a pointer
twice. Here is what I did:
class B {
// class definition.
};
class A {
B* b1;
B* b2;
A (const A& a) { // copy constructor (DEEP COPY) };
// Some get and set methods for b1, b2
void setb1(const B* b) {
if ( b1 )
delete b1;
b1 = b;
}
...
...
};
void myfunc(A* a) {
B* b = a->getb1();
delete b;
b = NULL;
B* c = new B();
a->setb1(c);
}
A* a = new A();
myfunc(a);
Now, after the call to myfunc(), if I did this:
A* a1 = new A( *a ); // Call the copy constructor
Then the pointers b1 and b2 inside a1 point to the SAME memory location!
The program worked as expected when compiled with the xlC
compiler and gave seg faults when compiled with g++ (3.4.1 and 4.0.0).
It is good that when I compile it with g++ I get an error (so that I
fix the buggy code), but it would be nice if g++ could catch it during
run time.
Thanks
Bala
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Catching memory errors
2005-05-22 2:06 Catching memory errors Bala B K
@ 2005-05-22 4:02 ` corey taylor
0 siblings, 0 replies; 2+ messages in thread
From: corey taylor @ 2005-05-22 4:02 UTC (permalink / raw)
To: Bala B K; +Cc: gcc-help
Bala,
I don't see a question or a point of failure. You'll have to be
more specific.
When did it crash? What is the copy constructor? What part is the
*strange behavior*?
corey
On 5/21/05, Bala B K <bkbsub@gmail.com> wrote:
> Hi,
>
> Recently I experienced a very strange behaviour with C++ (g++ 4.0.0,
> g++ 3.4.1 and g++ 3.3.3) when I made a mistake and deleted a pointer
> twice. Here is what I did:
>
> class B {
> // class definition.
> };
>
> class A {
> B* b1;
> B* b2;
>
> A (const A& a) { // copy constructor (DEEP COPY) };
> // Some get and set methods for b1, b2
> void setb1(const B* b) {
> if ( b1 )
> delete b1;
> b1 = b;
> }
> ...
> ...
> };
>
> void myfunc(A* a) {
> B* b = a->getb1();
> delete b;
> b = NULL;
> B* c = new B();
> a->setb1(c);
> }
>
> A* a = new A();
> myfunc(a);
>
>
>
> Now, after the call to myfunc(), if I did this:
>
> A* a1 = new A( *a ); // Call the copy constructor
>
>
> Then the pointers b1 and b2 inside a1 point to the SAME memory location!
>
> The program worked as expected when compiled with the xlC
> compiler and gave seg faults when compiled with g++ (3.4.1 and 4.0.0).
> It is good that when I compile it with g++ I get an error (so that I
> fix the buggy code), but it would be nice if g++ could catch it during
> run time.
>
> Thanks
> Bala
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2005-05-22 4:02 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-05-22 2:06 Catching memory errors Bala B K
2005-05-22 4:02 ` corey taylor
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).