Variable size isn't the reason to use a smart pointer, it's to allocation through construction and destruction of an object, rather than handling it manually.
In this case, stack allocation would probably be fine, something simple like auto buffer = std::array<unsigned char, 1000>(); but there's plenty of times where you want something of constant size allocated on the heap, and managing the allocation and deallocation of it with a smart pointer is a good way to do it.
Unique_ptrs have no perf hits, they behave basically the exact same as raw pointers with compile-time enforcements. Shared_ptr has a small perf hit just like any other reference counted pointer you'd implement.
Unique pointers can actually have a small performance hit on some architectures, because those always pass small structs on the stack, even when they fit in a register. But that only applies to function calls on some ABIs and in most cases it is not measurable. See https://libcxx.llvm.org/DesignDocs/UniquePtrTrivialAbi.html for more info.
As the other guy said they do have a perf hit but let's ignore that for now. If you allocate memory and use new and delete constantly with smart pointer what happens is (at least on windows) that you call new which calls malloc which calls HeapAlloc which does a lot of stuff and eventually calls VirtualAlloc and you just went through all that and then you go through basically the same thing with delete. A way better thing to do is to allocate a memory arena at startup and split that into sections, so you can have a permanent section that is always valid, a temporary section that is cleaned up at the end of a loop and maybe some special section that is cleaned up when something occurs. Now smart pointers are completely useless because the temporary memory handles itself and the permanent memory doesn't need to be freed and the cost of an allocation is incrementing a pointer and returning it's previous value, instead of 10 function calls that all do a billion things.
As I explained there is no reason to use smart pointers with custom allocators because the pointer just goes out of scope and the arena takes care of itself.
because you can guarantee that so easily working as a part of a team? have you ever worked in a larger team on a larger project? like seriously, what in-industry experience do you have?
there's a reason even the most knowledgeable c++ veterans advocate for the 5 reasons rule
as in if you don't have 5 very good reasons not to use smart pointers in a given situation, you should
Not to the end user, and I really don't feel like my performance is lower than the modern C++ "people" who put a million layers of abstraction before writing any code.
Learning how to code is basically Dunning and Kruger teaming up to beat the shit out of your self esteem with a baseball bat and a crowbar respectively.
Are there any actual Programming Mangas out there that are actually about Programing like New Game Is great but are there any that actually focuses on Programming.
I had a class a while ago that taught us C in the first half of the semester and C++ in the second half of the semester. Now I have a tutoring position for helping CS students and this is me frantically studying because I know what’s coming in March
When I was 9 years old, I got a Color Computer 2 for Christmas, complete with 64K Extended BASIC. I was plowing my way through the manual (for you zoomers out there, home computers came with fully documented guides to programming in BASIC), and I was like "this stuff's easy!" until I got to a certain point (I forget exactly what), and that's where 9-year-old me was like "whelp, looks like I'm sticking to making text games!"
102
u/yevepid Jan 09 '22
Manga is <New Game!>, has an anime adaptation - {New Game!}, both great
28
u/MonokelPinguin Jan 09 '22
If you get rid of the warnings, my understanding of the code falls by 1. (Code stolen from Vic on Twitter)
48
u/ThatsRightlSaidlt Jan 09 '22
void iGetIt() {
unsigned char * buffer = new unsigned char[1000];
delete[] buffer;
}
22
u/MandeoMana Jan 09 '22
The Dunning-Kruger effect strikes again!
16
u/[deleted] Jan 09 '22
Learning how to code is basically Dunning and Kruger teaming up to beat the shit out of your self esteem with a baseball bat and a crowbar respectively.
8
u/ooqq Jan 09 '22
We are all Nene
6
u/DemonCyborg27 Jan 09 '22
Are there any actual Programming Mangas out there that are actually about Programing like New Game Is great but are there any that actually focuses on Programming.
3
u/[deleted] Jan 09 '22
I had a class a while ago that taught us C in the first half of the semester and C++ in the second half of the semester. Now I have a tutoring position for helping CS students and this is me frantically studying because I know what’s coming in March
3
u/0x30507DE Jan 14 '22
I literally only program C++ because I have no will to live and genuinely hate my own existence.
</message>
2
u/sledgehammertoe Jul 27 '22
When I was 9 years old, I got a Color Computer 2 for Christmas, complete with 64K Extended BASIC. I was plowing my way through the manual (for you zoomers out there, home computers came with fully documented guides to programming in BASIC), and I was like "this stuff's easy!" until I got to a certain point (I forget exactly what), and that's where 9-year-old me was like "whelp, looks like I'm sticking to making text games!"
1
u/Salt_Bringer Jan 09 '22
I'm learning C++ through codeacademy. I feel the same way.
1
u/kirito_Abridged Jan 13 '22
That's exactly how it works
1
u/Osmiac Feb 10 '22
monads