Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!maverick.ksu.ksu.edu!iowasp.physics.uiowa.edu!ns-mx!ccad.uiowa.edu!cadsi Newsgroups: comp.lang.c++ Subject: Re: How to get the functionality of realloc() in C++ ? Message-ID: <1991May14.030205.25271@ccad.uiowa.edu> From: cadsi@ccad.uiowa.edu (CADSI) Date: 14 May 91 03:02:05 GMT References: <1991May13.191807.29295@csl.dl.nec.com> Organization: CAD-Research, U. of Iowa, Iowa City, Iowa Lines: 30 From article <1991May13.191807.29295@csl.dl.nec.com>, by manohar@csl.dl.nec.com (Mun o her): > How to get the functionality of 'realloc()' in C++ ? > Note : realloc() is used to change the size of the memory. > > The problem I have is as follows :- > > /** a part of prog */ > int i=0, data_func(); > > int *pt = new int[50]; > while(1){ > pt[i++] = data_func(); > <.... whenever the the array is filled up .... I want the array to double in > size. this is done using realloc() in C programs... realloc(pt,100) > > > } > Seems to me an Array class would be helpful here. Just overload the [] operator to reallocate the vector as necessary. malloc and realloc will work fine in C++. In addition, if you wanna use the new operator, you could allocate the new vector, copy the old into it, then delete the first. Otherwise, overload the new operator for the Array class to be smart about the re-allocation. |----------------------------------------------------------------------------| |Tom Hite | The views expressed by me | |Manager, Product development | are mine, not necessarily | |CADSI (Computer Aided Design Software Inc. | the views of CADSI. | |----------------------------------------------------------------------------|