Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.mixmin.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Ian Collins Newsgroups: comp.lang.c++ Subject: Re: HEAP error when trying to use free() help needed Date: Mon, 20 Jun 2011 17:17:13 +1200 Lines: 56 Message-ID: <9683f5FsmeU3@mid.individual.net> References: <75366d67-4c83-46cf-a75b-6a953578b6f8@v12g2000vby.googlegroups.com> <445a602a-ca39-4806-88bf-be5820678d7f@gv8g2000vbb.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net 5gpGCx5DMn115jMggT1f+AMTxsIFA8kXZv40JOf83Dz0k20N3L Cancel-Lock: sha1:v2fU9XSeheRYJb+fNkEhMInhiV8= User-Agent: Mozilla/5.0 (X11; U; SunOS i86pc; en-US; rv:1.9.2.9) Gecko/20101021 Lightning/1.0b2 Thunderbird/3.1.4 In-Reply-To: <445a602a-ca39-4806-88bf-be5820678d7f@gv8g2000vbb.googlegroups.com> Xref: x330-a1.tempe.blueboxinc.net comp.lang.c++:6919 On 06/20/11 05:06 PM, none wrote: > 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? -- Ian Collins