Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is C++ version of realloc(), to allocate the new buffer and copy the contents from the old one?

Tags:

In C we used malloc(), free(), but in C++ youare using new, delete, but in C we also have realloc, which will alloc the new block and copy the old data (common minimum) and then free the old data bock. So what is the C++ version of that? I can write my own of course, but is there a builtin thing?

main() {   int i; char *x = malloc(3);   x[0] = 10;   x[1] = 20;   x[2] = 30;   realloc(x, 4);   x[3] = 40;   for (i = 0; i < 4; i++) printf("%i\n", x[i]); } 
like image 466
exebook Avatar asked May 23 '13 13:05

exebook


People also ask

What is realloc function in C?

In the C Programming Language, the realloc function is used to resize a block of memory that was previously allocated. The realloc function allocates a block of memory (which be can make it larger or smaller in size than the original) and copies the contents of the old block to the new block of memory, if necessary.

Does realloc copy old data?

The value of realloc is the new address of the block. If the block needs to be moved, realloc copies the old contents. If you pass a null pointer for ptr , realloc behaves just like ' malloc ( newsize ) '.

How do I allocate memory with realloc?

Use of realloc() Size of dynamically allocated memory can be changed by using realloc(). As per the C99 standard: void * realloc ( void *ptr, size_t size); realloc deallocates the old object pointed to by ptr and returns a pointer to a new object that has the size specified by size.

What is realloc in C with example?

Use of realloc() in C CProgrammingServer Side Programming. The function realloc is used to resize the memory block which is allocated by malloc or calloc before. Here is the syntax of realloc in C language, void *realloc(void *pointer, size_t size)


2 Answers

There's no new/delete equivalent of realloc in C++.

From Bjarne Stroustrup's FAQ :

Why doesn't C++ have an equivalent to realloc()?

If you want to, you can of course use realloc(). However, realloc() is only guaranteed to work on arrays allocated by malloc() (and similar functions) containing objects without user-defined copy constructors. Also, please remember that contrary to naive expectations, realloc() occasionally does copy its argument array. In C++, a better way of dealing with reallocation is to use a standard library container, such as vector, and let it grow naturally.

If you want a resizeable container, just use std::vector, otherwise stay with malloc, realloc and free.

And, to answer your last question, the C++ version of your code would be :

main() {     std::vector<char> x(3);     x[0] = 10;     x[1] = 20;     x[2] = 30;     x.resize(4);     x[3] = 40;     for (int i = 0; i < 4; i++) std::cout << x[i] << std::endl; } 
like image 135
zakinster Avatar answered Sep 23 '22 23:09

zakinster


Let's see what Bjarne Stroustrup thinks!

Why doesn't C++ have an equivalent to realloc()?

If you want to, you can of course use realloc(). However, realloc() is only guaranteed to work on arrays allocated by malloc() (and similar functions) containing objects without user-defined copy constructors. Also, please remember that contrary to naive expectations, realloc() occasionally does copy its argument array.

In C++, a better way of dealing with reallocation is to use a standard library container, such as vector, and let it grow naturally.

like image 31
masoud Avatar answered Sep 25 '22 23:09

masoud