How do you realloc in C ++?

How can I realloc in C ++? It seems that the language is missing - there is new and delete , but not resize !

I need this because when my program reads more data, I need to reallocate the buffer to store it. I do not think that delete old pointer and new new, larger, is the right option.

+48
c ++ new-operator delete-operator realloc
Aug 14 '10 at 10:39
source share
4 answers

Use :: std :: vector!

 Type* t = (Type*)malloc(sizeof(Type)*n) memset(t, 0, sizeof(Type)*m) 

becomes

 ::std::vector<Type> t(n, 0); 

Then

 t = (Type*)realloc(t, sizeof(Type) * n2); 

becomes

 t.resize(n2); 

If you want to pass a pointer to a function, instead

 Foo(t) 

using

 Foo(&t[0]) 

This is absolutely correct C ++ code, because a vector is a smart C-array.

+34
Aug 14 '10 at 11:11
source share

The right option is probably to use a container that does your work, like std::vector .

new and delete cannot resize because they allocate enough memory to store an object of this type. The size of this type will never change. There is new[] and delete[] , but there is hardly ever a reason for using them.

What realloc does in C will most likely be just malloc , memcpy and free , although memory managers are allowed to do something smart if enough continuous free memory is available.

+38
Aug 14 '10 at
source share

Resizing in C ++ is inconvenient due to the potential need to call constructors and destructors.

I don't think there is a fundamental reason why in C ++ you couldn't have a resize[] statement with new[] and delete[] , something similar to this:

 newbuf = new Type[newsize]; std::copy_n(oldbuf, std::min(oldsize, newsize), newbuf); delete[] oldbuf; return newbuf; 

Obviously, oldsize will be retrieved from a secret location, the same thing happens in delete[] , and Type on the type of operand. resize[] will fail if the type is not copied - this is correct, since such objects simply cannot be moved. Finally, the above code creates objects by default, which you would not want to use as actual behavior, by default.

There is a possible optimization, where newsize <= oldsize , to call destructors for objects "past the end" of the newly created array and do nothing else. The standard would be to determine if this optimization is required (for example, if you resize() vector), resolved, but undefined, resolved, but implementation-dependent or forbidden.

The question you should ask yourself is "is it really useful to provide this, given that it also does vector and is specifically designed to provide a container with a resizable (contiguous memory - this requirement is omitted) in C ++ 98, but fixed in C ++ 03), which is better than arrays with C ++ ways of doing things?

I think the answer is widely considered no. If you want to make resizingable C buffers in a way, use malloc / free / realloc , which are available in C ++. If you want to make resizable buffers in C ++, use a vector (or deque if you really don't need continuous storage). Do not try to mix them with new[] for raw buffers unless you are implementing a vector container.

+27
Aug 14 '10 at 12:36
source share

try something like this:

 typedef struct Board { string name; int size = 0; }; typedef struct tagRDATA { vector <Board> myBoards(255); // Board DataBoard[255]; int SelectedBoard; } RUNDATA; 

Vector will complain. This is why arrays still exist, malloc and new.

-four
Jan 17 '16 at 20:30
source share



All Articles