Mail Archives: djgpp/1999/09/24/22:38:31
GAMMELJL AT SLU DOT EDU writes:
> zvector w1;
>
> zvector operator+(zvector w8,zvector w9)
> { int zi;
> for (zi=1;zi<length+1;zi++)
> {w1.s[zi+1]=w8.s[zi+1] + w9.s[zi+1];
> }
> return w1;
> }
>
> g=e+f;
This assignment will use default assignment operator, which will copy
all members from w1 to g. Your class contains pointer to dynamically
allocated memory, and after this assignment you have several classes
which contain the same pointer, which is then deleted several times in
destructors of those global objects (they are called before program
terminates). Freeing pointer twice may lead to crash, because free
does not check whether pointer is valid.
Try to add assignment operator `zvector &operator= (zvector&)' in
class declaration. And add copy constructor, just in case.
BTW, when you have problems with malloc, you can try one of packages
designed for debugging such problems. To name a few: mss, fortify,
yamd. Some of them might be found on simtel in djgpp directory,
others might be unsupported already.
--
Michael Bukin
- Raw text -