Why so much allocation on heap for vector with emplace back?
Hi I have a structure which has one int member. Size of this structure is hence 4 bytes. Now, when I have vector initialized with 3 objects of test structure, it is expected to allocate 12 bytes on heap. Global new operarator overloaded suggests this as expected. Now I am doing clear on vector and capacity remains 3 but size reduces back to 0. With this background, let me ask a query I have on my code snippet below: Now refer code of trial2 function. As i have not done shrink to fit yet, capacity does not go back to 0 and remains 3 yet. Now doing emplace_back 3 times and no allocation happens on heap. When at the end function call ends, vector object goes out of scope and de allocates everything from heap. This is perfectly fine for me to digest. Now, refer code of trial1 function. Here , i am just emplacing back 2 elements (obviously less than 3 i.e. capacity) but could observe allocation on heap... Why ? https://code.sololearn.com/cFRwdX6P4bjp/?ref=app