Sign in to follow this  
faculaganymede

C++ equivalent of "realloc"?

Recommended Posts

AFAIK, C++ does not have an equivalent. And no, new cannot be mixed with any of the C allocation functions (or rather, you can't delete a malloc-ed buffer or free a new-ed buffer, I guess you can use new and malloc in the same program as long as they are paired with the correct freeing method).

realloc was a bit of a crazy function really since there was no guarantee the memory would be left in the same place and just extended. I guess this is why there is no provision (that I am aware of) for it in C++.

Share this post


Link to post
Share on other sites
Quote:
Original post by faculaganymede
"new" in C++ is the equivalent of "malloc" in C.

No, the new operator creates a new object in C++. It has no direct equivant in C. In C++, ::operator new() is an analog for the malloc() function proivided in C, but really, the equivalent of malloc in C++ is spelled malloc().
Quote:

What is the C++ equivalent of "realloc"?

Try realloc().

If what you want to do is resize an array of objects created by the new[] operator, there is no way to do that. You would be better off upgrading your code from C-with-classes to C++ by using one of the dynamic containers supplied with the language (for example,std::vector) or available through third-party libraries.
Quote:

Can "new" be mixed with "realloc"?

You can use them freely in the same application. You cannot use realloc() to reallocate the memory allocated to an object created with the new operator, neither can you use free() with a pointer holding a value assigned from the new operator or ::operator new(). Also, you cannot use the delete[] operator on an object created with the new operator, ::operator new(), malloc(), calloc(), or realloc(). I could name more thing you canot do, but suffice it to say your should use match allocation or creation with the appropriate deallocation or destruction, and never mix them up.

Share this post


Link to post
Share on other sites
Quote:
Original post by Bregma
...


You're mixing up the operator new and new-expression. new int; is an example of new-expression (and not "new operator") -- it does two things: 1) allocates storage via operator new; 2) constructs an object in this storage.

Also, to the OP: the second step in new-expression is the key difference between raw memory allocation functions, like malloc and operator new(), and the reason you can't mix them up.

Share this post


Link to post
Share on other sites
Quote:
Original post by Bregma
Quote:
Original post by faculaganymede
"new" in C++ is the equivalent of "malloc" in C.

No, the new operator creates a new object in C++. It has no direct equivant in C.

I and others read it to mean (ObjectType*)malloc( sizeof(ObjectType) * N ) in C, although it's good to point out the differences like the majority of your post does.

Quote:
Quote:

What is the C++ equivalent of "realloc"?

Try realloc().

...but actually encouraging it's use is rather mean :o

Share this post


Link to post
Share on other sites
Redux: there is a difference between strict equivalence in the C++ language (in almost all of these cases, you just use the C version directly, because it's been kept around for backwards compatibility), and moral equivalence in C++ style. If you need the behaviour provided by realloc() in C but you want to actually make good use of the language's power, use the standard library class std::vector. This neatly avoids countless headaches with doing the memory management yourself. Really, if there were no such thing in the standard library, the only sane way to make sure you handled every weird case (especially allowing for exceptions) properly would be to re-implement it. Fortunately, we are luckier than that.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this