I do not know yet how to pass unique_ptr as reference to a function. void whatever(...,std::unique_ptr &engine); - - - The call: whatever(engine). It should work, but do not compile.
shared_ptr is for sharing between threads, i.e. when handling shared data to another thread. Within a thread it's too slow because the control-block is atomically updated.
handmade hero is a great series on youtube. it uses c++ to make a game from scratch and teaches how to properly manage memory etc. it starts with the windows platform layer and how to do cross platform proper.
11:27 cracked up so hard. I do this all of the time when making videos. Edit: wth? How did that even compile at 13:12. You have to declare the data type when making a shared pointer. Weird.
"How did that even compile at 13:12. You have to declare the data type when making a shared pointer." - Since C++ 17 there are deduction guides which allow weak_ptr to take over the type from the shared_ptr and the other way around.
Hi Caleb i ran shared pointer example.its giving error . error:smart_p3.cpp:6:34: error: conversion from ‘’ to non-scalar type ‘std::shared_ptr’ requested 6 | std::shared_ptr a= std::make_shared; during compilation . which c++ compiler ur using.. I tried this on g++: gcc version 9.4.0 Please guide Thanks Abhishek
The level of abstraction is on a really akward level. On one hand yeey nice, you don't have to delete pointers manually. On the other hand, if you need to do anything more advanced like cloning, deep and or shallow copy and storing in containers, that requires even more work, knowledge and frustration than managing raw memory directly. I find using smart pointers in very simple situations, where it's not necessary at all and in complicated situations I use raw pointers, just because I hate thinking about the obscure behavior of smart pointers.
Smart pointers are a fairly thin wrapper in terms of added complexity. It's not a huge difference in understanding to use them as opposed to raw pointers, and the benefits of clearly defined ownership and automatic deallocation is monumental for the little abstraction. To me though, something that bothers me is just how verbose it is. Going from * to std::shared_ptr is a pain in the ass. Especially when you're making collections of smart pointers or the like
what I find weird about this code is that you are allowed to return a unique_ptr from a function without using the move function. Is seems that the compiler is smart enough to realize you are moving the unique_ptr from the function to the caller of the function.
More proof that cpp is a total mess... Your mistake at 11:35 quite hilarious.. And then weak pointers. Lol. Personally, I'll stick to remembering delete... 😂
Don't use auto in production code, the rest is good, but auto can make bad assumptions when you start getting into nested data types and other techniques that come up in production code. auto is dangerous and could lead to bad assumptions by the compiler.
I hope this was helpful! Here is the course link I mentioned in the video. New C/C++ course: calcur.tech/c-cpp-newsletter
I do not know yet how to pass unique_ptr as reference to a function.
void whatever(...,std::unique_ptr &engine);
- - -
The call: whatever(engine).
It should work, but do not compile.
As somebody who learned and uses mainly C++98, I'm glad to see videos about these newer C++ concepts I often forget about. 👍
let's go. This is what we need. Been using your lessons for learning anything. So far been great. thanks.
shared_ptr is for sharing between threads, i.e. when handling shared data to another thread. Within a thread it's too slow because the control-block is atomically updated.
Very well done. There were other videos on smart pointers that left me a bit confused. Yours is clear & understandable. Thank you
handmade hero is a great series on youtube. it uses c++ to make a game from scratch and teaches how to properly manage memory etc. it starts with the windows platform layer and how to do cross platform proper.
As great as always. Cannot wait to see the course.
Thanks!
Thanks Caleb. Very clear examples and explanations.
You’re a beast. Been watching for interview prep. You should start an udemy course!
This video is very helpful, it's make me figured it out in one video huge thanks!
11:27 cracked up so hard. I do this all of the time when making videos.
Edit: wth? How did that even compile at 13:12. You have to declare the data type when making a shared pointer. Weird.
"How did that even compile at 13:12. You have to declare the data type when making a shared pointer."
- Since C++ 17 there are deduction guides which allow weak_ptr to take over the type from the shared_ptr and the other way around.
Best video for smart pointers
Great bro🎉
Very clear explanation
Great tutorial! Thank you
Hey good presentation Caleb!
hey bro. Please do a java springboot video or course?
Hi Caleb
i ran shared pointer example.its giving error .
error:smart_p3.cpp:6:34: error: conversion from ‘’ to non-scalar type ‘std::shared_ptr’ requested
6 | std::shared_ptr a= std::make_shared;
during compilation . which c++ compiler ur using..
I tried this on g++: gcc version 9.4.0
Please guide
Thanks
Abhishek
You have it should be (5)
It looks like it thinks you are trying to use a function for the type because you put the () inside the
thanks alot caleb(@@codebreakthrough ) it work for me..
Why you using camel case btw and more can you make a video how to setup an editor for c++ development
Awsome. Thanks!
cystall clear!
The level of abstraction is on a really akward level.
On one hand yeey nice, you don't have to delete pointers manually.
On the other hand, if you need to do anything more advanced like cloning, deep and or shallow copy and storing in containers, that requires even more work, knowledge and frustration than managing raw memory directly.
I find using smart pointers in very simple situations, where it's not necessary at all and in complicated situations I use raw pointers, just because I hate thinking about the obscure behavior of smart pointers.
Isn’t that what smart pointers?
Smart pointers are a fairly thin wrapper in terms of added complexity. It's not a huge difference in understanding to use them as opposed to raw pointers, and the benefits of clearly defined ownership and automatic deallocation is monumental for the little abstraction.
To me though, something that bothers me is just how verbose it is. Going from * to std::shared_ptr is a pain in the ass. Especially when you're making collections of smart pointers or the like
Thanks!
what I find weird about this code is that you are allowed to return a unique_ptr from a function without using the move function. Is seems that the compiler is smart enough to realize you are moving the unique_ptr from the function to the caller of the function.
This is one of the best coding tutorial videos I've watched. Clear, concise and complete. Thank you so much for your great work!
Ownership, lifetime,... After learning some basic Rust, all of these make sense. 😀🦀
As a user, you shouldn't be using new/delete anymore. As a library developer, you need it to implement RAII.
4:39 WRONG! 2 memory leaks.
P.S. Love the content
More proof that cpp is a total mess... Your mistake at 11:35 quite hilarious.. And then weak pointers. Lol. Personally, I'll stick to remembering delete... 😂
bro thinks hes mcoding💀💀
Absolutely ridiculous... simply put variable a outside main!
Don't use auto in production code, the rest is good, but auto can make bad assumptions when you start getting into nested data types and other techniques that come up in production code. auto is dangerous and could lead to bad assumptions by the compiler.