views:

221

answers:

5

In C the standard memory handling functions are malloc(), realloc() and free(). However, C++ stdlib allocators only parallel two of them: there is no reallocation function. Of course, it would not be possible to do exactly the same as realloc(), because simply copying memory is not appropriate for non-aggregate types. But would there be a problem with, say, this function:

bool reallocate (pointer ptr, size_type num_now, size_type num_requested);

where

  • ptr is previously allocated with the same allocator for num_now objects;
  • num_requested >= num_now;

and semantics as follows:

  • if allocator can expand given memory block at ptr from size for num_now objects to num_requested objects, it does so (leaving additional memory uninitialized) and returns true;
  • else it does nothing and returns false.

Granted, this is not very simple, but allocators, as I understand, are mostly meant for containers and containers' code is usually complicated already.

Given such a function, std::vector, say, could grow as follows (pseudocode):

if (allocator.reallocate (buffer, capacity, new_capacity))
  capacity = new_capacity;     // That's all we need to do
else
  ...   // Do the standard reallocation by using a different buffer,
        // copying data and freeing the current one

Allocators that are incapable of changing memory size altogether could just implement such a function by unconditional return false;.

Are there so few reallocation-capable allocator implementation that it wouldn't worth it to bother? Or are there some problems I overlooked?

A: 

I guess this is one of the things where god went wrong, but I was just too lazy to write to the standards committee.

There should have been a realloc for array allocations:

p = renew(p) [128];

or something like that.

Pavel Radzivilovsky
If you use vectors instead of arrays, there's `.reserve()`. Why add new functionality for arrays when vectors are generally better?
David Thornley
+4  A: 

Because of the object oriented nature of C++, and the inclusion of the various standard container types, I think it's simply that less focus was placed on direction memory management than in C. I agree that there are cases that a realloc() would be useful, but the pressure to remedy this is minimal, as almost all of the resulting functionality can be gained by using containers instead.

tlayton
I'm not sure I agree they thought about it less because of OOP. Placement new is an example of a feature specifically for memory management of objects.
Joseph Garvin
I'm not saying they thought about it less, just that the actual syntax was designed to place more emphasis on OOP than on direct memory management in cases where the two are different options. Placement new is a perfect example of where the two are used together by the programmer rather than one replacing the other.
tlayton
Actualy placement new is the apparatus that *enables* otherwise very hard custom memory management (specifically memory pools) within c++ facilities. I think of it as a device that *encourages* customizing memory management with c++ code libraries.
Ofek Shilon
+6  A: 

What you're asking for is essentially what vector::reserve does. Without move semantics for objects, there's no way to reallocate memory and move the objects around without doing a copy and destroy.

Mark Ransom
A good usecase for such functionality for this would be sparse containers. Using vectors, especially with preallocated memory, in them would completely defeat their purpose (sparseness is meant to conserve memory).
doublep
@doublep: If you wanted sparse containers neither (dynamically) allocated arrays nor vectors are what you want.
Martin York
@Martin York: Well, for instance Google Sparsehash library uses dynamically allocated arrays and achieves very nice results.
doublep
@doublep: I am sure it does use dynamically allocated arrays. But it does not use "A" dynamically allocated array as it is a bit more complex than that.
Martin York
@Mark Ransom: If you have a class similar to vector (using placement new/manual destruction, except memory allocated with malloc) would there be any problem calling realloc on the backing store? Or using new create a resized backing and memcpy the existing objects in. As long as you don't access both copies of your backing memory wouldn't the shallow copy be fine?
Hayman
@Martin York: I don't understand what you said at all.
doublep
+6  A: 

From: http://www.sgi.com/tech/stl/alloc.html

This is probably the most questionable design decision. It would have probably been a bit more useful to provide a version of reallocate that either changed the size of the existing object without copying or returned NULL. This would have made it directly useful for objects with copy constructors. It would also have avoided unnecessary copying in cases in which the original object had not been completely filled in.

Unfortunately, this would have prohibited use of realloc from the C library. This in turn would have added complexity to many allocator implementations, and would have made interaction with memory-debugging tools more difficult. Thus we decided against this alternative.

5ound
+4  A: 

This is actually a design flaw that Alexandrescu points out with the standard allocators (not operator new[]/delete[] but what were originally the stl allocators used to implement std::vector, e.g.).

A realloc can occur significantly faster than a malloc, memcpy, and free. However, while the actual memory block can be resized, it can also move memory to a new location. In the latter case, if the memory block consists of non-PODs, all objects will need to be destroyed and copy-constructed after the realloc.

The main thing the standard library needs to accommodate this as a possibility is a reallocate function as part of the standard allocator's public interface. A class like std::vector could certainly use it even if the default implementation is to malloc the newly sized block and free the old one. It would need to be a function that is capable of destroying and copy-constructing the objects in memory though, it cannot treat the memory in an opaque fashion if it did this. There's a little complexity involved there and would require some more template work which may be why it was omitted from the standard library.

std::vector<...>::reserve is not sufficient: it addresses a different case where the size of the container can be anticipated. For truly variable-sized lists, a realloc solution could make contiguous containers like std::vector a lot faster, especially if it can deal with realloc cases where the memory block was successfully resized without being moved, in which case it can omit calling copy constructors and destructors for the objects in memory.

+1 A link for backup: http://www.stepanovpapers.com/notes.pdf
Joseph Quinsey