delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/09/24/22:38:31

To: djgpp AT delorie DOT com
Subject: Re: problem with new malloc.c see note at bottom attn: Eli Zaretskii
References: <01JGCJLWUJ7A9I4LWV AT SLU DOT EDU>
From: Michael Bukin <M DOT A DOT Bukin AT inp DOT nsk DOT su>
Date: 25 Sep 1999 07:44:22 +0700
In-Reply-To: GAMMELJL@SLU.EDU's message of "Fri, 24 Sep 1999 12:12:55 -0500 (CDT)"
Message-ID: <20g103ddll.fsf@Sky.inp.nsk.su>
Lines: 32
X-Mailer: Gnus v5.5/Emacs 19.34
Reply-To: djgpp AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019