Dynamic Memory Management in C++

>2018
Is there really any justification for not using smart pointers Jow Forums?

They do everything that normal pointers can but without pajeet memory leaks and don't have very noticeable overhead.

Is there any case where you'd realistically want to use a regular pointer other than to feel like you have control?

Attached: Selection_999(218).png (582x217, 27K)

Other urls found in this thread:

youtu.be/xnqTKD8uD64?t=744
twitter.com/NSFWRedditVideo

Implementing your own smart pointer system of course.

No. Just learn it to pass your intro classes and make it a habit to use smart pointers afterwards. It'll help you learn about dynamic memory allocation, hopefully.

But why would does that have that the STL Smart pointer system doesn't

From what I’ve read, raw pointers express intent. If a class has one, you’re saying “this object has ownership over this pointer and is responsible for its lifetime” or something like that.
I’m not sure, I may be wrong. It’s just something I heard

what's a smart pointer and how is it different than a garbage collector?

Optimization for freaky alien game systems.

IDK, you tell me. I don't see the point. If using smart pointers is preventing memory leaks for you, don't you now technically have a bug in your code that you'll never notice?

>what's a smart pointer and how is it different than a garbage collector?

A smart pointer is just a wrapper for a standard pointer but it sets the pointer itself to private and just overloads the -> operator and defines a destructor that performs deallocation correctly so that way when the variable referencing the smart pointer goes out of scope, the memory space gets freed up automatically

>sets the pointer itself to private
How do you do that?

What? No. The proper deallocation is handled for you so when you're writing larger projects, a little mistake isnt gonna fuck you up and cost you time trying to debug it

>a little mistake isnt gonna fuck you up and cost you time trying to debug it
That's what bothers me about them.

>Is there really any justification for not using smart pointers Jow Forums?
The project you're adopting doesn't and updating the spec isn't part of the requirement.
Also, why can't we get a new C++ standard that's clean and modern? Every time I try to learn it and work on existing code I always find like 12 ways to the same thing and it frustrates the hell out of me. It's the only language I've tried that I've never committed to learning because of how fractured the practices are for even the simplest things

smart pointers are deterministic in the sense that every time an object reaches 0 reference counts it's deallocated, garbage collector scans the memory for un referenced blocks, the time it does it and the specific conditions are somewhat tricky, that means you can never tell if an object is de allocated at any point in time.

Smart pointers are a good way to implement immutable data structures in C, otherwise you need to pay a full copy each time you pass a large object for modification and don't want to leak state.

>value semantics
>rule of 5 / rule of three if you like copy and swap idiom
>std::vector/std::string
>STL
>function objects
>lambdas

That's pretty much it, the other stuff is just for C compatibility and for boomers

That's what I found about C++ too. All the good stuff right there.

template
class smart_ptr {
private:
Type *data;

public:
smart_ptr(const Type* allocated_data = nullptr) : data(allocated_data) { }

~smart_ptr(void) {
delete data;
}

Type& operator*(void) {
return *data;
}

Type* operator->(void) {
return data;
}

};


Basically like this

>2018
Is there any justification for not using C instead of C++?
It does everything C++ can without pajeets (at all).
Is there any case where you'd realistically want to use C++ over C other than to feel like you need the features (bloat)?

Although I forgot to put in a lot of stuff that I normally would like default constructor, constant access, copy, etc

>being this retarded

>Is there any case where you'd realistically want to use C++ over C other than to feel like you need the features (bloat)?


Yeah a lot actually, the ability to overload operators, the ability to define certain functions for Objects that perform better with an OO model rather than functional while still retaining the entire functionality of C. It's not bloated in the slightest and really provides a full suite of tools to use instead of having to fuck around and try to implement an array each time or download one off your github and shit like that

The advent of templates has also made it far more powerful in what you can do with it.

Cleaner code if you use it right (main "spine" of the code is still C, but you use classes to organize stuff).
C++ is only shit when you follow one of those literal religions of EVERYTHING MUST BE AN OBJECT.

Yep that'll do.

>he doesn't design a new language and compiler for every new project

Attached: 1517246588998s.jpg (117x125, 2K)

That would have to be for optimization purposes

Yeah I think if you have exact specifications for how something should be in your project then go for it but most of the time what's defined by the compiler that you're using is really going to be close to the most optimized code you could probably get

>Is there any case where you'd realistically want to use a regular pointer other than to feel like you have control?
Pointing to non-dynamically allocated objects.

>Is there any justification for not using C instead of C++?
It's an objectively better language.
>It does everything C++ can without pajeets (at all).
Oh, nice, oh can I write template specializations in C?
Also, stop with this C++ pajeet meme. You niggers somehow believe C++ is like Java or C# because basedb0ys make C++ tutorials on the internet using new all the time like a retard, and refuse to actually learn the language.
I despise Cniles more and more every day.

At this point I could be using Lisp

Smart pointers look uglier than raw pointers. Still I often use them in bigger projects, but for small and quick stuff it doesn't matter.

Yea i mean like for big projects where it's a problem not something where its just unnecessary

There’s no way using a smart pointer to index blocks of allocated data would make any sense.

Here, I think you'll enjoy this talk.

Around the 12 minute mark is what you're lookin for.
youtu.be/xnqTKD8uD64?t=744

>Is there really any justification for not using smart pointers Jow Forums?

Absolutely.
Smart pointers are for managing ownership. If you want to give non-ownership access to an object to another function or class, in cases where references don't work (STL containers can't contain references, to start with...) it's fine, and in fact necessary, to pass raw pointers.

You also need raw pointers, of course, when implementing your own data structures.

I wouldn't use smart pointers for your tight inner programs loop.

>Is there really any justification for not using smart pointers Jow Forums?
Only in extremely rare cases.
However, that doesn't mean you should just shared_ptr everywhere, that's retarded.

>Is there any case where you'd realistically want to use a regular pointer other than to feel like you have control?
1) Dealing with C APIs
2) Memory you're not managing, for example mapped IO memory.

The only dynamic thing about C++ is the sweet release of death

Attached: file-15~01.jpg (452x364, 22K)

Foo::Initialize()
{
delete this;
}

Smart pointers are already obsolete, m8. Stop reading those 4 year old books.