|
|
|
|
|
by pmelendez
4049 days ago
|
|
The link that you provided does not have to do with malloc and/or free but with perfect forwarding: http://thbecker.net/articles/rvalue_references/section_07.ht... Also that example uses a Factory pattern which is actually verbosing the example, but it's like comparing apple and oranges since that's OOP which you actually wouldn't be doing in C. What you would be comparing is something like: mystruct * stcVar = malloc(sizeof(mystruct));
free (stcVar); Vs. std:shared_ptr<mystruct> stcVar; Of course that is a stupid example; However things gets more interesting when you have pieces of code sharing the same structure (threads maybe?) and you don't know exactly which code should be the one in charge of releasing the pointer. |
|
clearly, it is C++, not C.
instead of a simple malloc and free, you will invariably end down the rabbit hole learning about rvalues. perfect forwarding, move semantics, lvalues - the list goes on - these topics arise from the simple concept of RAII.
which i find worse than malloc and free.