I know this is a pretty common question, but I do not understand the code examples from related questions (Too long to work into, too complicated). So, here goes my simple Code snippet:
#include <iostream>
#include <memory>
#include <vector>
using namespace std;
int main(){
vector<int> v({1,2,3,4,5});
shared_ptr<vector<int>> v_ptr(&v);
cout << (*v_ptr)[2] << endl;
}
Now, this short snippet creates a memory leak. I think this is caused by destructing the vector before destructing the shared_ptr. However, I do not understand how to destroy the pointer accordingly. Simply trying reset() won't work, so what can I do, and why is there a memory leak?
Please, don't ask me why this is needed, I am new to smart pointers and trying to understand how to use them. This example is independent from my work, however, I want to avoid memory leaks in the future, so this question might actually help. Thanks in advance!