Good videos. One clarification, in this ex. the pointer variable "p" will be created on stack so its local in main() ,but the chunk of memory will be allocated on heap. So once we get exception , the pointer "p" goes out of scope hence memory leak because we lost pointer "p" and thus lost the access to chunk of memory on heap.
good video explaining the idea behind unique_ptr and how to use it (it's like the non-managed C++ version of garbage collection for raw pointers). The only issues I had were that your examples can be a bit hard to follow at first. You know what you intend to show but it's a little confusing, even for a veteran programmer, to follow what you're doing when passing ownership around, releasing pointers, etc. But then that's generally difficult to follow in C++ at the best of times. Maybe more descriptive variable names, including the initial values, e.g. unique_ptr fooptr1_10 = make_unique(10); Then when you e.g. unique_ptr fooptr2_10 = std::move(fooptr1_10) you can see that the contents of fooptr1_10 were moved. Dumping the values of objects after moves to show that the object really was transferred from one place to another might help people unfamiliar with the concept too.
Dude you are making good videos. Just one suggestion, it is hard to read on mobile screen whatever you have written on the terminal. Better if you can increase the font size or zoom the window.
I am getting this error on doing this - std::unique_ptr t1 = new Test (2000) ; error : conversion from 'Test*' to non-scalar type 'std::unique_ptr' requested Please help!!!!! Why doesn't = work here?
Great video, easy to understand. i still got a problem tough.. i'm trying to allocate a private member vector of unique_ptr to a base class object ( std::vector m_objects ; ) and than to pushback derived classes into it trough a void function that gets nothing and returns nothing but it knows the vector member in the private section of the class because the cpp file belongs to its header file so i use ( m_objects.push_back(std::make_shared (); ) but i always get these linkage errors 2019 and 1120 and i don't know what to do anymore..
universal initialization using {} is the preferred method. The C++ committee has gone through many attempts to get initialization of all types of object correct, and it still has problems in certain circumstances. Nicolai Josuttis (C++ committee member and prolific book writer and conference presenter) informs us that {} should be used everywhere, even to the extent of doing e.g. for (int i{0}; i < limit; ++i) ... I think most people (including me) will continue to write for (int i = 0; ...) in that instance but everywhere else, for local, member, and global/static variable initialization, {} is a good habit to get into.
@sara That's what my point is. In your example 2 your program is not exiting main, while 1 will keep looping indefinitely inside main. My doubt is Int main() { Int *p = new int(10); } Is it a memory leak after program exits? I believe no! OS takes away memory allocated to your a.out
@@13taras The point of calling delete everytime is to make most efficient use of memory allocated to the program. If we free memory, the freed memory can then be reused by our program and we do not run out of memory.
I thought destructors are called automatically when scope ends. Why is destructor not called in your first run? It has something to do with stack/heap allocation? I'm guessing that when you declare class object with new, only the reference is destroyed at end of scope, not the object. And destructors are called only when the object is destroyed.
Sir I want to do specialization in a language. Which language should I prefer for campus placement and for future . Is java better or c++ or other one.
What happen if I do delete p after p3.get(): Foo *p = p3.get(); delete p; what happens with p3? Is it still point to the object in which it was returned?
It still points to the adress in memory, that you deleted. Your program will crash when the p3 gets out of scope and the deleter is called, cause there is nothing left to delete.
@@JayAnAm Did you try to execute? It doesn't crash. p is a pointer of type Foo *. It is not a unique pointer, and it holds the address to the object of p3, so p and p3 are 2 different entities. Did you ever see a destructor called for a object pointer being deleted?
Not bad, but you have to be a bit more careful when explaining things like move, espcially when your viewers are not familiar with lvalues and rvalues. std::move doesnt move anything, it is casting into rvalue. And p1 is not a nullpointer after std::move, it is an empty object, just the raw pointer that is wrapped by p1 is nullptr.
@@JayAnAm I took Rupesh (?) to mean that the contained raw pointer was set to nullptr after the move, but then I know the subject matter. Making it crystal clear and unambiguous to less experts wouldn't hurt.
std::move : cast object into rvalue get: returns the raw poiner, wrapped by smart pointer reset: Replaces the object managed by the smart pointer release : Releases the ownership of the managed object
If I have a class with unique_ptr as a member, How to write a copy constructor for this class??? class A { std::unique_ptr< int > up_; public: A( const A& a ) {}; }
I think you no need to be over conscious of your voice part..... because of this drama, your video content does not come out well. Concentrate on content and good explanation, instead of making singing kind of voice.