In Custom vector, part 1, we have created a vector class that will add any new element and resize to any limit (at least till your app goes out of memory).
Now, lets try it with a class object, which will store elements on heap, here we can track added items constructor/deconstructor calls.
Here, we have created a wrapper class on int, where it will keep hold for a integer value.
We have overridden multiple constructors and deconstructors, we log these events to understand more of what happens internally.
add fist
Constructor called for 35, address: 0x7ffd588620c4
Default Constructor called for 0, address: 0x560e071ddec8
Deconstructor called for 35, address: 0x7ffd588620c4
value: value: 35, address: 0x560e071ddec8
add second
Constructor called for 30, address: 0x7ffd588620c4
Default Constructor called for 0, address: 0x560e071ddee8
Default Constructor called for 0, address: 0x560e071ddeec
Deconstructor called for 35, address: 0x560e071ddec8
Deconstructor called for 30, address: 0x7ffd588620c4
value: value: 35, address: 0x560e071ddee8
value: value: 30, address: 0x560e071ddeec
add third
Constructor called for 25, address: 0x7ffd588620c4
Default Constructor called for 0, address: 0x560e071ddec8
Default Constructor called for 0, address: 0x560e071ddecc
Default Constructor called for 0, address: 0x560e071dded0
Default Constructor called for 0, address: 0x560e071dded4
Deconstructor called for 30, address: 0x560e071ddeec
Deconstructor called for 35, address: 0x560e071ddee8
Deconstructor called for 25, address: 0x7ffd588620c4
value: value: 35, address: 0x560e071ddec8
value: value: 30, address: 0x560e071ddecc
value: value: 25, address: 0x560e071dded0
pop back
value: value: 35, address: 0x560e071ddec8
value: value: 30, address: 0x560e071ddecc
add fourth
Constructor called for 20, address: 0x7ffd588620c4
Deconstructor called for 20, address: 0x7ffd588620c4
value: value: 35, address: 0x560e071ddec8
value: value: 30, address: 0x560e071ddecc
value: value: 20, address: 0x560e071dded0
Deconstructor called for 0, address: 0x560e071dded4
Deconstructor called for 20, address: 0x560e071dded0
Deconstructor called for 30, address: 0x560e071ddecc
Deconstructor called for 35, address: 0x560e071ddec8
Here, lets look at the output:
As we look at the logs,
We create a new object using Parameterised Constructor.
Then we check if size == capacity, here it comes yes.
Now we create an array of size 1, filled with default object.
After creating new array, we call default constructor to create a new object to fill it initially.
Then, we copy all elements from previous array to new array with increased size.
After copying old array, now we can delete it from memory as all elements are safe in new array.
Repeat 2 to 6 till we have added all elements.
As we see, we do a lot of memory operations for storing new data into vector.
Here are steps for each cycle:
Create multiple objects.
Copy them to new memory location.
Delete old data.
This consume precious memory and CPU cycle for simple operations, in next lesson, we will see how we can optimise this.