+ 2

Why vector does not call heap allocation

Hi https://www.sololearn.com/compiler-playground/cqFgWopfA64b Why heap allocation is not happening on this code? I have created one object into vector and thought that allocation is on heap for class object. Why so? Am I missing something?

27th Nov 2022, 6:52 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
12 Réponses
+ 2
This is because you haven't passed any custom allocator to your vector and default allocator uses global "new" and "delete" operators to allocate your memory. See this: https://code.sololearn.com/cKZJC4JPo9Z2/?ref=app
28th Nov 2022, 3:17 AM
Arsenic
Arsenic - avatar
+ 3
Ketan Lalcheta something to do with new? I never dig this deep, just how to properly use something is enough for me.😁 https://www.daniweb.com/programming/software-development/threads/382895/stl-vector-and-malloc
27th Nov 2022, 8:24 AM
Bob_Li
Bob_Li - avatar
+ 2
KrOW and Bob_Li , i am also not having dare to do allocation customely 😝😝 I am just trying to observe heap allocation and de allocation in normal scenario of vector. Studying this as one of my task is to identify performance lag and i feel that issue is frequent allocation on heap... i might be wrong also but thats why trying to understand this in details.
28th Nov 2022, 1:34 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Ketan Lalcheta new operator overload is called when you create an object with "new" (really??? 😁) but in your code you creating the object without new (then onto stack)... Thats all
27th Nov 2022, 8:35 AM
KrOW
KrOW - avatar
+ 1
KrOW but vector elements are called on heap... right ? Vector itself is on stack but its container for elements is on heap... Right ?
27th Nov 2022, 10:37 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Ketan Lalcheta from my meager understanding, you are basically correct https://stackoverflow.com/questions/8036474/when-vectors-are-allocated-do-they-use-memory-on-the-heap-or-the-stack but you can change the behavior by writing your own custom allocator. Just thinking about having the need to do optimizations like these is numbing my mind.. you might as well program it in assembly language.😂
27th Nov 2022, 11:01 AM
Bob_Li
Bob_Li - avatar
+ 1
Ketan Lalcheta Yes, the vector items are created on the heap. Vector object (eg. the direct internal data) is allocated onto stack but some data refer to data onto heap... Just for make all simple: int* a= new int; "a" pointer is onto stack but it refers to data onto heap.
27th Nov 2022, 11:04 AM
KrOW
KrOW - avatar
+ 1
Bob_Li Yes, custom allocator is not for every day use but for optimizing some scenarios... A good reading is here https://indiegamedev.net/2022/03/27/custom-c20-memory-allocators-for-stl-containers/
27th Nov 2022, 11:17 AM
KrOW
KrOW - avatar
+ 1
KrOW 😵😵 optimization is a deep dark rabbit hole. Not for the weak-hearted .... General knowledge is good enough for me.😅 Ketan Lalcheta, good luck on your quest for the dark magic...😱
27th Nov 2022, 11:33 AM
Bob_Li
Bob_Li - avatar
+ 1
The global new operator is called by library triggered by vector internal allocation not by vector construction (thats is always did onto stack so the temp testtt object passed as parameter)... Just to be clear. Anyway dont overload new/delete globally if you dont know what it do
28th Nov 2022, 12:18 PM
KrOW
KrOW - avatar
+ 1
Thanks Arsenic for solving my doubt
28th Nov 2022, 1:35 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
😂
27th Nov 2022, 12:12 PM
KrOW
KrOW - avatar