+ 1
About dynamic memory allocation and management in C
I created a simple code which implements vector concept in C++. Is my code really the implementation of vectors in C++? If not, how is then vectors implement in C++? Also, is there any memory leaks in my code? https://code.sololearn.com/cxCsw64CCvoY/?ref=app
4 Answers
+ 2
Looks ok I think.
Note that C++ vectors don't reallocate every time you add an element, rather they reallocate into a bunch of extra space at once because allocating is expensive. In addition to size, they have a "capacity", which is how much you can add before another realloc happens.
How often a vector reallocs is implementation defined. I remember hearing that many implementations double the capacity on each realloc but I never tested it.
0
Martin Taylor oh sorry sorry I forgot to do the main part and you're right. Please see my code again. Now I've corrected it. actually I forgot to do that before
0
Schindlabua oh okay. Thank you for your comment