views:

397

answers:

4

I'm aware that there is a realloc function that would allow me to resize the memory block (and it's paired with a free function). However, I'm trying to do the same to a c++ class with some member pointers allocated memory using new instead of realloc. Is there an equivalent keyword to realloc in c++ that would allow me to achieve the same goal when the memory is paired using new/delete rather than malloc or realloc and free?

Thanks in advance.

+3  A: 

No, there isn't. And frankly, if you are using new or new[]. your C++ code is probably not well designed. Look at using std::vector instead of new[], and at using values instead of new.

anon
There is nothing wrong with using an ordinary "new" in C++ code, although it should probably be used in conjuction with boost::shared_ptr or one of the other BOOST smart pointer classes.
Michael Aaron Safyan
There is nothing wrong with it if you know what you are doing. But using values should be the first choice.
anon
Sometimes values aren't a viable option (e.g. a C++ object which is shared between multiple objects).
Michael Aaron Safyan
well use a vector of shared_ptr :)
AraK
+2  A: 

I am not aware of if there is one or not, but you could probably use a vector as you are just resizing an array of elements :)

std::vector<char> iVector(1000); // 1000 element as initial size

iVector.resize(2500); // resize the vector to 2500 elements

// or just use it without worrying about memory management
AraK
A: 

No. Just use malloc/realloc/free, what's problem?

stepancheg
The problem with malloc/realloc/free in C++ is that they are not guaranteed to invoke constructors and destructors correctly, whereas using the new and delete keywords guarantee correct construction/destruction.
Michael Aaron Safyan
anon
Call them yourself then.
toto
+1  A: 

If I understand your problem, you have a class which allocate some variable-length memory. There is two possible cases:

The memory contains C++ objects

You have no choice: The memory should be, directly or indirectly allocated with new because you need the constructors called.

If you want to have a realloc-like behaviour, then do not use new[]. Use instead a std::vector, which will handle all the allocation/reallocation/Free and construction/destruction correctly.

The memory is raw

You could either use new[] or malloc, because you are allocating PODs (an array of ints, or shorts, dumb structures, etc.).

But again, new[] won't offer you a realloc-like behaviour... But if you start using malloc, then you must do housekeeping, i.e. be sure to call free at the right time, memorize the size of the array somewhere, and perhaps even have a size different from the array capacity (i.e. you allocate more, to avoid doing too much reallocs).

And you know what? std::vector already do all this for you.

Conclusion

You are coding in C++, then the solution to your problem (i.e. allocating variable length memory inside a class), as already expressed by previous answers, is use std::vector.

paercebal