Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use std::vector instead of realloc? [closed]

Here, in this question, it's stated that there is no realloc-like operator or function in c++. If you wish to resize an array, just just std::vector instead. I've even seen Stroustrup saying the same thing here.

I believe it's not hard to implement one. There should be a reason for not implementing one. The answers only say to use std::vector but not why it's not implemented.

What is the reason for not implementing realloc-like operator or function and preferring to use std::vector instead?

like image 493
StackExchange123 Avatar asked Sep 08 '26 20:09

StackExchange123


1 Answers

What is the reason for not implementing realloc-like operator or function and preferring to use std::vector instead?

Save time. Don't chase bugs in your own code for a problem that has long been solved. Idiomatic C++ and readability. Get answers to your questions easily and quickly. Customize the realloc part by an allocator.

I believe it's not hard to implement one

That heavily depends on what you need from the template you intend to write. For a general-purpose std::vector-like one, have a look at the source code (libcxx's 3400 line vector header is here). I bet you will revise you initial assumption on the low complexity of such construct.

like image 170
lubgr Avatar answered Sep 12 '26 18:09

lubgr