delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/09/25/00:02:23

From: "Damian Yerrick" <web DOT poison AT pineight DOT 8m DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: problem with new malloc.c see note at bottom attn: Eli Zaretskii
Date: Fri, 24 Sep 1999 22:05:40 -0500
Organization: Rose-Hulman Institute of Technology
Lines: 38
Message-ID: <7she9o$jkn$1@solomon.cs.rose-hulman.edu>
References: <01JGCJLWUJ7A9I4LWV AT SLU DOT EDU> <20g103ddll DOT fsf AT Sky DOT inp DOT nsk DOT su>
NNTP-Posting-Host: yerricde.laptop.rose-hulman.edu
X-Trace: solomon.cs.rose-hulman.edu 938228856 20119 137.112.205.146 (25 Sep 1999 03:07:36 GMT)
X-Complaints-To: news AT cs DOT rose-hulman DOT edu
NNTP-Posting-Date: 25 Sep 1999 03:07:36 GMT
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.00.2314.1300
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Michael Bukin <M DOT A DOT Bukin AT inp DOT nsk DOT su> wrote in message
news:20g103ddll DOT fsf AT Sky DOT inp DOT nsk DOT su...
> 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.

Whenever you free() a block through a pointer, set the pointer to
NULL just to be safe.

I also see a fence post error. This code copies all elements from s[2] to
s[length + 1].
Remove the +1's and change zi's initial value to 0; then write back.

--
Damian Yerrick
http://pineight.webjump.com/



- Raw text -


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