Date: Wed, 5 Jan 1994 11:28:13 -0500 (EST) From: Linh Minh Hong Dang Subject: Re: Complex new/delete bug To: Richard Young Cc: djgpp AT sun DOT soe DOT clarkson DOT edu On Tue, 4 Jan 1994, Richard Young wrote: > BUG REPORT FOR PC-GNUC VERSION 2.41 > ----------------------------------- > > I have found a bug when creating complex arrays with new and then deleting them. The > deletion of the complex array does not seem to free up the memory that was allocated and > a succession of new/delete will eventually result in an insufficient memory error. > > I have been able to circumvent the bug by refraining from using new complex[n]. > In place of > complex * x = new complex[n]; > I use > complex * x = (complex *)malloc(n*sizeof(complex)); > > The following test program, which I named testmemf.cc, illustrates the problem. I have > listed the various tests that I ran below: > > testmemf 1 using new complex[n], will eat up memory and die, note new'ed ptr increasing > > testmemf 2 no problem with double arrays, ptr not increasing > > testmemf 3 no problem with single complex objects, ptr not increasing > > testmemf 4 no problem with malloc(n*sizeof(complex)) instead of new complex(n) > > testmemf 5 seems to show that the amount of memory allocated with new is OK, > that is, the delta between ptrs is n*sizeof(complex) bytes > > testmemf 6 same delta as test 5 but I found it odd when the address of the 1st ptr > here was consistently different than in test 5 > > Although I am avoiding the problem for now I would appreciate if someone could locate and > fix the bug. > > Thanks and Merry Christmas!!! > > Richard Young, DSP engineer > Communications Research Centre > 3701 Carling Ave., > Ottawa, Ontario > Canada > K2H 8S2 > (tel) (613)998-2235 > (fax) (613)990-6339 > (e-mail) richard DOT young AT crc DOT doc DOT ca > > //-------------------- testmemf.cc -------------------------------------------------------- > > #include > #include > #include > > int main (int argc, char *argv[]) > { > if (argv[1][0] == '1') // successive new/delete with complex arrays > { > for (;1;) > { > complex * x = new complex[1000]; > printf("%c x=%p\n",argv[1][0],x); > delete x; delete[] x; > } > } > else if (argv[1][0] == '2') // successive new/delete with double arrays > { > for (;1;) > { > double * x = new double[1000]; > printf("%c x=%p\n",argv[1][0],x); > delete x; > } > } > else if (argv[1][0] == '3') // successive new/delete with single complex objects > { > for (;1;) > { > complex * x = new complex; > printf("%c x=%p\n",argv[1][0],x); > delete x; > } > } > if (argv[1][0] == '4') // successive new/delete with malloc'ed complex array store > { > for (;1;) > { > complex * x = (complex *)malloc(sizeof(complex[1000])); > printf("%c x=%p\n",argv[1][0],x); > delete x; > } > } > if (argv[1][0] == '5') // to check amount of memory allocated to each new array > { // using new > complex * x = new complex[1000]; > complex * y = new complex[1000]; > complex * z = new complex[1000]; > printf("%c x=%p y=%p z=%p\n",argv[1][0],x,y,z); > } > if (argv[1][0] == '6') // to check amount of memory allocated to each new array > { // using malloc > complex * x = (complex *)malloc(sizeof(complex[1000])); > complex * y = (complex *)malloc(sizeof(complex[1000])); > complex * z = (complex *)malloc(sizeof(complex[1000])); > printf("%c x=%p y=%p z=%p\n",argv[1][0],x,y,z); > } > return 0; > } > > //-------------------- end of testmemf.cc -------------------------------------------------- > > >