About dynamic memory allocation and management in C | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

16th Jun 2021, 11:34 AM
Rishi
Rishi - avatar
3 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.
16th Jun 2021, 11:02 PM
Schindlabua
Schindlabua - avatar
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
16th Jun 2021, 3:24 PM
Rishi
Rishi - avatar
0
Schindlabua oh okay. Thank you for your comment
17th Jun 2021, 2:19 AM
Rishi
Rishi - avatar