vendredi 20 décembre 2019

C++ Shared pointer points to the wrong address when i use make_shared function from stl

So i have a class in which there is a shared_ptr declared as following

std :: shared_ptr< T > dyn_arr{ new T[ dyn_arr_max_size ], std :: default_delete< T[] >() };

This points to the dynamic array of some size. I also implemented an iterator for it. Inside this iterator there is a ++ overloaded operator. Now when i get

shared_ptr<T> ptr_iter= dyn_arr; 

for example it will work for the first one or two elements. After that it does not iterate properly. Also i notices the following:

For example my ptr_iter is address ABDC0 for ptr_iter.get() in the beginning After doing

ptr_iter = std :: make_shared<T>( *(ptr.get() + 1 ) );

or

ptr_iter = std :: make_shared<T>( ptr.get()[1] );

ptr_iter.get() will point to some other address now like SDBC instead of pointing to ABDC4 for integers for example. Can someone please explain me why is this happening???

Aucun commentaire:

Enregistrer un commentaire