Subj : Re: realloc() issue on C++ To : borland.public.cpp.borlandcpp From : José Mauro Date : Sun Mar 20 2005 05:40 pm Yes, std::vector is the clean solution for a new code. However, my problem is practical. I have 15000 lines of ANSI C code, converted to C++ and making use of templates to apply the algorithm in custom data types. The original malloc() and free() calls were converted to new [] and delete [], to guarantee constructors and destructors calls for each array element. For the realloc() calls, i need a way to expand the memory, now obtained through the new [] operator. Someone know a "quick & dirty" way to solve this problem? "Thomas Maeder [TeamB]" > "Jose Mauro" writes: > > [please wrap lines] > >> There are a short and efficient way to implement the "C" realloc >> function (where the previous content is copied to the new location >> if necessary) in C++ ? > > realloc() is also a C++ function. > > Do you mean an equivalent to realloc() for memory obtained through > ::operator new()? There is no such equivalent. > > >> Stroustrup book's recommend the use of vector<> in this case, but >> will be very cumbersome rewrite my code to use STL containers. >> >> Basically, i have a lots of "Object *" arrays allocated with "new >> Object[size]", and i need reallocate these arrays to, say, >> "Object[newsize]", preserving the initial contents. > > It's also very cumbersome to get these allocations (and, more > importantly, the appropriate deallocations) correct, unless you use a > container class, isn't it? > > >> My difficulty is that initial array "size" is not know (not in >> scope) ate the reallocation point, for permit a memory copy. > > Another point in favor of std::vector. .