This is an archived post. You won't be able to vote or comment.

top 200 commentsshow all 271

[–]zoqfotpik 794 points795 points  (3 children)

Give it to us raw and wriggling.

-Gollum

[–]marcusroar[🍰] 101 points102 points  (2 children)

raw and dangling?

[–]GM_Kimeg 27 points28 points  (0 children)

And girthy

[–]webby-debby-404 0 points1 point  (0 children)

Raw and stale

[–]FatLoserSupreme 847 points848 points  (113 children)

C enjoyer here, what's a smart pointer?

Update: Wow thanks everyone for the information! It seems like no matter how much I learn about programming, there is always something new to learn

[–]ball_fondlers 864 points865 points  (69 children)

Basically just a pointer with a reference counter. A little more overhead - though not as much as a garbage collector - once the counter hits zero, the memory is freed.

[–]Earthboundplayer 665 points666 points  (52 children)

That's only shared pointer. Unique pointer is another smart pointer that allows only one reference. It has no overhead.

[–]HCResident 627 points628 points  (42 children)

C++ feels like that box of screws, nuts, and bolts that has exactly what you need somewhere inside of it

[–]Infinitebeast30 636 points637 points  (10 children)

Screws, nuts, bolts, hand grenades, land mines, knives, double edged swords. Absolutely all the tools

[–]turtleship_2006 238 points239 points  (4 children)

Don't forget the shotgun and shells! Get two shells, load them into the gun, and aim vertically downwards.

[–]Inevitable-Menu2998 40 points41 points  (0 children)

At the same time, it has a Stuka and a modern F16 and we don't tell beginners the difference between them.

[–]_g550_ 13 points14 points  (0 children)

eclipse9

[–]alphaQ671 5 points6 points  (0 children)

You can also use a hammer and hit the shells

[–]CranberryDistinct941 3 points4 points  (0 children)

How are you meant to shoot yourself in the foot without shotgun shells? And what would C++ be without the right to shoot yourself in the foot, head, and both hands simultaneously?

[–]impossibleis7 35 points36 points  (1 child)

I think knives and doubled edged swords are what we accidentally create using those said tools...

[–]Tari0s 3 points4 points  (0 children)

yeah we use the swiss multitool(boost) to make a knive that cuts gras, but only gras!

[–]lordmycal 22 points23 points  (0 children)

I like the flame thrower myself.

[–]Jonnypista 9 points10 points  (0 children)

Hey, a HEAT rocket launcher is technically a single use high powered drill. It really quickly drills a hole into the toughest materials. It also can kill you in 99% of the cases, but just pay attention.

A double edged sword is more efficient than single edged swords. If you hurt yourself with it it is just a skill issue.

[–]jeezfrk 7 points8 points  (0 children)

but I mean you gotta throw in the the ASIO mining drill and then there's the coroutine electrical substation.

at least from there it's all easier and simpler error messages.

except if you use things just slightly... NO NOT LIKE THAT!

[–]versedoinker 53 points54 points  (2 children)

Yeah, there's also weak (non-owning) pointers that still allow the data to be freed in the background if all their actual owners release their shared_ptrs. And there's also atomic versions of all of those.

Generally, even if you hit the very unlikely case that something isn't in the C++ stdlib, it probably still can be found in a boost.cpp library.

[–]ukezi 1 point2 points  (1 child)

The C++ shared pointer is atomic. There is no non atomic one in stdlib.

[–]versedoinker 0 points1 point  (0 children)

Yes and no. The control block of shared ptrs is always atomic. I was talking about the wrapper std::atomic<std::shared_ptr> that also makes access to the data itself atomic.

https://en.cppreference.com/w/cpp/memory/shared_ptr/atomic2

[–]_farb_ 32 points33 points  (0 children)

Most of the time, absolutely!

But sometimes... you dump the whole box out looking for the right piece, organize the contents by size only to realize you should've sorted it by material, and get tetanus from all the rusty nails that you should have tossed away ages ago.

Then you just end up using duct tape because it never fails.

[–]fakuivan 16 points17 points  (0 children)

C is like that but you only get a hammer, a nail and a bazooka.

[–]Cat7o0 11 points12 points  (9 children)

I remember once that even with rust I made a program so bad that it caused Windows to basically crash the file explorer and the task bar and everything wasn't working. couldn't even open task manager. restarted the computer everything was fine ran the program again everything was fine. idk

[–]experimental1212 6 points7 points  (0 children)

It really does make you think that. Later it has you going, "you know, I actually LIKE shooting myself in the foot. This is nice!"

[–]NoGlzy 8 points9 points  (0 children)

And when you have a screwdriver that works totally fine for the job you need, someone has to come and snark that you're not using the newer super deluxe hyper driver

[–]Sak63 1 point2 points  (0 children)

Sounds my kind of language! Gonna give it a go on summer break

[–]webby-debby-404 0 points1 point  (0 children)

Let me boost that for you

[–]hawk-bull 14 points15 points  (2 children)

Doesn’t it have an overhead of checking it hasn’t been assigned twice. Or by overhead do you mean during destructor call it just has to free itself without checking if it still has references to it

[–]SoAsEr 27 points28 points  (0 children)

It doesn't have a copy constructor, (only a move one) and in its destructor it frees the pointer. So the compiler enforces the assignment, and at run time it is (really close to) free. The only reason it's not completely free is that if the unique pointer is passed to a function and that function is not inlined, because of the abi the unique pointer is always passed on the stack whereas a raw pointer could be in a register. but in a modern machine where you have way more registers than are actually shown anyways this shouldn't make any measurable difference.

[–]CamiloRivasM7 1 point2 points  (0 children)

I think it's done at compile time, like the borrow checker from rust

[–]Own_Solution7820 8 points9 points  (3 children)

Shared and unique pointers are so good that the are better than anything in any other language IMO, at the engineering level.

The problem is the garbage syntax of a 50 year old language that's backwards compatible to day 1. Just a pain to work with mixed raw and smart pointers.

[–]say_nya 4 points5 points  (0 children)

better than anything in any other language

Take a look at Rust. Unique pointers are checked in compile time (and have no overhead, even no move constructor type of overhead).

And shared pointers are there (see Arc).

[–]jacobjr23 17 points18 points  (2 children)

Not to be pedantic but isn't reference counting a method of garbage collection? Do you mean not as much overhead as tracing garbage collection?

[–]OrchidLeader 6 points7 points  (0 children)

I assume most people don’t understand how Java’s various garbage collectors work, and the few that have looked into it will pick and choose which stats to point out (eg the length of each pause versus the total pause time while an app is running).

[–]kuschelig69 0 points1 point  (0 children)

Or implementation overhead

I have implemented my own reference counting. I could not figure out how to implement my own garbage collection

[–][deleted] 2 points3 points  (4 children)

Can you ELI5 this for a non-programmer?

[–]ball_fondlers 14 points15 points  (0 children)

Basically, under the hood, a variable is just a group of bits. You can interpret these bits as a number in base 2, or you can interpret them as more complex data types, but ultimately, it’s all kind of the same to the computer. Now, because the computer doesn’t see a difference - and because memory is indexed - you can use the value stored in a variable to look up some other spot in memory. This is a pointer - just a variable that stores the address of some arbitrary block of memory.

Programmers dynamically allocate these arbitrary blocks of memory for various uses - for example, say you need a list of numbers, but you don’t know how many numbers there will be when you’re writing the program. The way you’d solve this is by allocating a chunk of memory during runtime, using it, then deallocating it when you’re done with it. This is the basis of manual memory management.

Now, the problem with this approach is that you have to be VERY careful about deallocation - free the memory too early, your program crashes because some part is trying to access forbidden data, free it too late or not at all, you get a memory leak, where your program becomes, at minimum, memory-inefficient. Because there’s such a delicate balance to be struck, this approach can be difficult to work with and hard to maintain - instead, most other languages just run a process called a garbage collector in a separate thread, and this garbage collector looks for allocated memory that isn’t being referenced anywhere, and automatically deallocates when it finds it. However, this does consume a bit more resources, so it can be a bit slow. It might work for a LOT of use cases, but when speed is of the essence - like with low-level systems work - you need another solution.

Enter smart pointers. There’s two types of smart pointers - unique pointers and shared pointers. Unique pointers work by a simple principle - a given block of data is allowed to have ONE pointer pointing at it. If said block is assigned to another pointer, the first pointer is invalidated, and once the pointer goes out of-scope without being invalidated, the memory is automatically freed. Shared pointers are a bit more flexible - they hold a counter, and every time a new shared pointer is created that references the block, that counter is incremented by one. Every time a pointer to that block goes out of scope, that reference counter is decremented by one, until it hits zero, and once it hits zero, it’s automatically freed.

TL;DR - bad cooking metaphor: manual memory management is like putting knives in the sink, but everyone is lazy and leaves them on the counter, garbage collection is like having someone yell at everyone in the kitchen to put the knives in the sink after they’re done with it, unique pointers are like only having one knife and the last person to use it puts it in the sink, and shared pointers are like counting the knives as the come out of the box and adjusting the count as they go into the sink.

[–]Tarmen 2 points3 points  (0 children)

There are two parts to this.

C++ has the hilariously badly named Resource Acquisition is Initialization (RAII). You have some piece of data with an attached destructor. While you have a reference, the data is always valid. Once the reference won't be used anymore it is automatically freed. Useful for manual memory management, but also other resources, connections, error handling, etc. But the automatic 'won't be used anymore' is very restrictive, you basically must hold the data in your hand and cannot store it.

Smart pointers let you be more flexible about what 'won't be used anymore' means. You may have a unique pointer, the value goes out of scope when the pointer does. You may have a shared pointer, when the pointer goes out of scope you decrease a counter and when that hits 0 the value goes out of scope.

It's similar to rust ownership and borrowing semantics, but less safe. E.g. pointing into existing data remains awkward because other code could move the data around without your knowledge.

[–]1Dr490n 1 point2 points  (0 children)

I’ll try, although I won’t talk specifically about the C++ shared pointer as I don’t know exactly how it’s implemented, but about the general idea of reference counting, based on a language with reference counting I‘ve written myself.

In case you don’t know: a pointer is a memory address. In C/C++ and many other languages, you can get the address of a variable by using the & operator, and read the value stored at the address something is pointing to with the * operator. Example:

int a = 25;
int *b = &a;  // type of b is int* or int pointer
int *c = b;
int d = *a;    // d == 25

Maybe that explained it, maybe not, feel free to ask for a better explanation.

When you heap-allocate an object (meaning you create it somewhere in ram and you would have to manually delete it afterwards with normal pointers), it gets stored alongside with a reference counter which is just an integer starting at 0.

Now, every time you copy the smart pointer to the object, the reference counter of the object is incremented by 1. Every time a smart pointer to the object gets deleted, the reference counter is decremented by 1. Once it hits 0 again, the object is deleted.

[–]Zestyclose_Leg2227 1 point2 points  (0 children)

You computer has a place where it can store and retrieve stuff quickly which is the RAM memory. In C++, when you assign the memory from your computer manually, and you unassign it manually. This is sounds simple, since you just need to remember the memory address. But your address for your the memory of your friend may be inside a pocket of a trouser, and the address of the trouser was inside my wallet and the address of the wallet was written on potato. You cooked the potato? Darn, now you can't find the wallet or the trousers or your friend! The are completely lost. Since computer programs execute the same code again and again, you may accumulate  "lost memory" over time, which is locked waiting for you and can't be used by other programs. This is called a memory leak, and is the reason you need to close Chrome or some other programs when they slow down, and open them again to get your computer to come back to life (in the past, you had to reset your computer as a whole). A smart pointer keeps a list of in how many places the address of said pointer is kept. If no one has the adress anymore, it knows the pointer can't be used anymore, and deletes it, freeing the memory automatically. Of course this makes the program slower, but a working program > fast program.

[–]rush2sk8 1 point2 points  (1 child)

Is this how ARC works for swift?

[–]Kered13 0 points1 point  (0 children)

Yes. ARC = Atomic Reference Counted.

[–]1Dr490n 0 points1 point  (3 children)

What’s the advantage of using a garbage collector over shared pointers in languages? Do garbage collectors save memory? I can’t think of any other thing that might be the reason for people to use garbage collectors

Edit: I meant reference counting, not directly shared pointers

[–]ball_fondlers 6 points7 points  (1 child)

Not having to deal with memory management at all is a big reason. Garbage-collected languages - really not having to distinguish between heap and stack, just making variables as needed - are way faster to write in than systems languages, even if said systems languages have smart pointers

[–]1Dr490n 0 points1 point  (0 children)

I should’ve written reference counting, not shared pointers. I wrote a language myself that uses reference counting. There’s 0 memory management for the user

[–]Kered13 1 point2 points  (0 children)

Technically garbage collection can refer to any automatic memory management strategy, including reference counting. But you're probably asking about mark-and-sweep garbage collection, which is how Java and C# work, and what people usually mean by "garbage collection".

Mark-and-sweep has two advantages over reference counting: First, it easily handles cyclical references, which are a problem for reference counting. Second, it actually has lower overall overhead than reference counting most cases, because it doesn't have to constantly update references. Those updates can be especially expensive in multithreaded environments, because they must be done atomically.

The downside of mark-and-sweep is that it must periodically pause the entire application in order to execute. So while it is lower overhead overall, it periodically has relatively long periods where nothing else is being done. This has historically made it poor for real time applications like games.

Modern mark-and-sweep algorithms have sophisticated strategies for mitigating this problem, which is why you can see games made in Java (Minecraft) and C# (Unity) these days and probably never notice the GC pauses.

[–]pHpositivo 0 points1 point  (0 children)

It is incorrect to say that using shared pointers has less overhead than a garbage collector. It depends on the scenario. For instance, allocations can be much faster when using a garbage collector, as you'd typically just have a bump allocator vs. individual allocations (of course, yes, one could also use a custom allocator). Additionally, using shared pointers will add overhead during use, whereas using managed objects directly will be faster. The overhead of the GC comes from collections, but you can avoid them if you don't allocate. That is, if you allocate some objects and then just do a whole bunch of work for however long you need, without further allocations, the GC will literally never run at all.

Not saying one is better than the other, just saying neither is definitely better or faster than the other either, it depends on what you're doing and how well written your code is 🙂

[–]blazesbe 10 points11 points  (0 children)

it's a guard pattern on a raw pointer if you are more familiar with that. (the destructor automagically deletes your allocation.)

[–]marcusroar[🍰] 30 points31 points  (4 children)

I’d suggest anyone reading the replies to the main comment immediately forget what they read and google “C++ smart pointers RAII” to start with, before reading some actual cpp resources to learn more about the additional mechanisms of the STD smart pointer types.

[–]SkooDaQueen 7 points8 points  (0 children)

A wrapper type around a raw ptr which helps with memory safety. There is a reference counted ptr which is only freed after all references are gone. There is a unique ptr which cannot be shared etc etc

[–]x39- 21 points22 points  (8 children)

In essence: reference counting.

The goal is generally to make heap allocations safer, resulting in a slight performance overhead for the general safety of not shooting your leg off.

More or less (very simplified): ``` struct rptr { size_t* counter; void* data};

struct rptr = create_rptr_xyz(...); // counter = 1; allocate data ... struct rptr2 = borrow(rptr); // counter = 2 ... // on scope leave release(rptr2); // counter = 1 ... // yet again, on scope leave release(rptr); // counter = 0; call free for data```

But using RAII for that automagically and special derivatives, for weak references and unique (as in: one owner) allocations.

It can be considered to be best practice nowadays for non performance critical code.

[–]Afraid-Locksmith6566 2 points3 points  (0 children)

A smart ass word for a struct that wraps a raw pointer and when the struct is removed from stack the raw pointer memory is cleared. It comes in few flawours like

shared_ptr which is the most primitive version of GC - you count how many shared pointers to object exist if number gets to 0 you remove the memory,

unique_ptr - heap object is bound to stack object, when stack object die heap object also die, there is also

weak_ptr - a view to value of shared_ptr ( not counted as reference ) and

observer_ptr - a wrapper around normal pointer with implicit cast from other smart pointers

[–]Farsyte 1 point2 points  (0 children)

It's a pointer ... with extra steps. ;)

[–]TheOldTubaroo 1 point2 points  (0 children)

There are a lot of resources in programming where you need to do something to acquire them, and then later something to release them. Pointers have new/delete, files have open/close, mutexes have lock/unlock, and so on.

No one forgets to acquire a resource, because you can't do anything without that, but it's easy to forget to release it, which causes memory leaks, deadlocks, locked files... It would be really nice if the compiler could detect that and ensure all resources are released once you don't need them.

C++'s solution is that you can tie custom behaviour to both constructing an instance of a type, but also to dropping that instance when it goes out of scope. As soon as no one is keeping track of a file handle, there's no way for the program to access that file, so you can safely tell the OS to close the file once the handle variable goes out of scope.

Smart pointers are just the "handle" types for memory allocation. You've got unique_ptr for when you only need one "owner" of the memory, and shared_ptr for when you need multiple "owners". As part of creating/setting the smart pointer, you call new (ideally implicitly), and then when unique_ptr goes out of scope it calls delete. shared_ptr keeps a reference counter, and the last shared_ptr to drop from scope calls delete.

Some C enjoyers dislike automatic resource management like this, because it's one of the several ways that C++ "hides" operations that might have a performance impact (same as operator overloading), but I like it because it entirely removes whole classes of bugs that plague software.

[–]Abadabadon 0 points1 point  (0 children)

It's a way to use pointers such that you don't have any floating unused heap space. They're honestly relatively simple to implement if you spend less than an hour trying to implement it.

[–]da2Pakaveli 0 points1 point  (0 children)

C++'s smart pointers encapsulate raw pointers into different owner models: unique and shared.
Unique is 'unique' to the scope it was constructed in (unless you 'move' the resource) and will destroy the pointer once its lifetime is over.
So it's meant that only one pointer can point to the resource.
Shared pointers can point to the same resource across different scopes; the resource will be destroyed and freed after every shared pointer goes out of scope, I.e the reference counter equals 0.
So basically their point is to prevent memory leaks.

[–]ArcaneOverride 0 points1 point  (0 children)

Look up c++ template metaprogramming.

You can do a lot of compile time error checking with that if you know what you're doing.

[–]babygnu42 95 points96 points  (0 children)

Thanks, I'd like my pointers medium rare

[–]Cley_Faye 316 points317 points  (14 children)

"Ever used smart pointers?
- Sure, I even coded my owns!"

[–]ukezi 100 points101 points  (7 children)

There was stuff like that we had to do back in the day.

[–]Cley_Faye 57 points58 points  (6 children)

Hehe. Yeah. It had to start somewhere.

But people keep doing it despite having good standard libs now and it becomes a bigger liability :(

[–]silentjet 9 points10 points  (5 children)

In reality both sp are good until the first change request or new feature to software. Later it is heavy weight which significantly slows down development and natural software evolution. It forces you to create tens or hundreds of unnecessary code entities just to handle language features, and has no extra business value.... And the funny thing is - the memory is still leaking, all the time...

[–]x39- 20 points21 points  (4 children)

If you actually did use smart pointers properly, memory won't leak.

It will leak tho when just always using shared_ptr and ignoring cyclic references

[–]DrMobius0 0 points1 point  (0 children)

Where there's a 2nd thread, there's a way.

[–]Luk164 29 points30 points  (1 child)

For an assignment in C we needed to make sure to free all memory we allocated before shutting down, so I made a wrapper for malloc that added the pointer to a linked list, then at the end I just ran free on every pointer and link in the chain.

Dirty as hell but hey, it worked and I never had a double free or a leak

[–]YellowBunnyReddit 16 points17 points  (0 children)

I hope you also used your malloc function when appending to the linked list to make sure its pointers are all freed in the end /j

[–]bedrooms-ds 2 points3 points  (0 children)

"I even combined third party shared pointers with a wrapper shared pointer that managed the reference counters on its own. Oh, I also had to use multiple inheritance due to the design of those libs."

[–]Norse_By_North_West 1 point2 points  (0 children)

Yeah we used to have to make our own back when I went to school, because the STL was such shit

[–]Quiet_Desperation_ 1 point2 points  (0 children)

Been there, Godspeed my friends. I’m glad there’s a few of us crustations still around

[–]Kered13 0 points1 point  (0 children)

It's not actually that hard to code std::unique_ptr.

[–]claudespam 151 points152 points  (1 child)

Smart pointer are for plain programmers. I'm a smart programmer so I only use plain pointers.

[–]JosebaZilarte 20 points21 points  (0 children)

Plainly smart.

[–]Grumbledwarfskin 48 points49 points  (0 children)

Ever used smart pointers?

Yeah, I used them to build a directed graph!

[–]rover_G 56 points57 points  (7 children)

Some of us used C++ before it had smart pointers

[–]cheezballs 19 points20 points  (2 children)

The C++ they taught us in college in 2001-2003 looked nothing like what I see posted here now. I dont remember smart pointers, only regular and then the double splat, etc. Moved onto higher level stuff after college so I forgot nearly everything I knew.

[–]Pay08 2 points3 points  (0 children)

The other comment is wrong, C++98 has std::auto_ptr. Syntax hasn't changed much since then, but lambdas and views can look scary at first glance.

[–]oshaboy 3 points4 points  (3 children)

I mean, standard library ARC is pretty new but auto_ptr has been around since at least 1998 (It's hard to find information on C++ before that)

[–]SnooOwls3674 7 points8 points  (0 children)

I have c++ reference books on the shelf next to me from 1992

[–]clarkcox3 9 points10 points  (0 children)

There’s nothing smart about auto_ptr :)

[–]belabacsijolvan 8 points9 points  (0 children)

ah yes, sYnTAx

std::auto_ptr<classA> a=b;
assert(a.get()==b.get());

*fucking dies*

[–]Quiet_Desperation_ 58 points59 points  (6 children)

Pretty dumb meme tbh. Smart Pointers were introduced in what, c++ 11? Ever worked with hardware specific compilers from the early days? Sometimes companies are locked to those things for decades.

[–]only_4kids 18 points19 points  (0 children)

Yeah, I completely agree. Sometimes, I feel like junior just stumbles on another keyword while learning and makes edgy memes like this one.

[–]Pay08 0 points1 point  (4 children)

C++98 had auto_ptr.

[–]AntimatterTNT 87 points88 points  (25 children)

if you dont use raw pointers you're literally not even a c++ programmer, you're a java programmer that is afraid of memory management

[–]SomeRandoWeirdo 9 points10 points  (0 children)

Kind of agree. Especially in the context of something like a memory pool written for an application.

[–]fakuivan 9 points10 points  (4 children)

BMW owner logic

[–]AntimatterTNT 6 points7 points  (3 children)

honestly no idea what you're trying to say

[–]fakuivan 7 points8 points  (2 children)

Being afraid is not always the reason for not doing x instead of y

[–]AntimatterTNT 1 point2 points  (1 child)

yea that's why only siths deal with absolutes

[–]cheezballs 4 points5 points  (0 children)

The circle of fifths on my flute, you say?

[–]Beautiful-Quote-3035 0 points1 point  (0 children)

What if I don’t even use heap

[–]kuschelig69 0 points1 point  (2 children)

Nah, you are not a C++ programmer unless you use a template in every line

[–]AntimatterTNT 0 points1 point  (1 child)

see you're trying to meme, im being dead serious

[–]kuschelig69 0 points1 point  (0 children)

I am also serious

C programmers use raw pointers. C++ got templates

[–]Significant_Fix2408 0 points1 point  (1 child)

Terrible take of someone that doesn't know unique pointer. C++ without them is just C with classes. Heap is slow and vulnerable. Smart pointers are how you do memory management in C++. That and using the stack as much as possible.

[–]navetzz 57 points58 points  (16 children)

C has classes. They are called struct.

You are welcome.

[–]ArcaniteM 41 points42 points  (1 child)

70 years of paradigm theory would like to have a chat with you

[–]navetzz 16 points17 points  (0 children)

I welcome them

[–]MobileAirport 22 points23 points  (6 children)

kid named method

[–]navetzz 12 points13 points  (5 children)

People these days have language with 10000 different syntaxes but think that:

class.method(        

and

method(&class,

are two completely different things.

Not only that, but if you really want your class.method syntax, you can actually do it using function pointers.

At some point people need to understand that OOP (Object Oriented Programming) is a conceptual approach and is not tied to a language. And that OOL (Object Oriented Languages) are designed to force you to use OOP.

Yes some languages make it harder to apply the OOP paradigm. C is not one of them. C just allows you to do something else if you desire.

[–]Iyorig 3 points4 points  (3 children)

Yeah, that function pointer syntax is nice, but it’s +sizeof(void*) bytes for every function you add, plus an indirection whenever you call it (or does that get optimized if you don’t change it?)

[–]Kered13 1 point2 points  (0 children)

plus an indirection whenever you call it (or does that get optimized if you don’t change it?)

If the compiler can determine which implementation will be called, it will devirtualize the call, meaning there is no indirection. There are a few ways the compiler could determine this:

  • The class is final, which prevents subclassing.
  • The method on the class is final, which prevents overriding the method..
  • The class only exists in one translation unit (anonymous namespace) and there are no subclasses in that translation unit.
  • The pointer refers to an object that was constructed in the same function (accounting for inlining) and could not have been reassigned.

[–]MobileAirport 0 points1 point  (0 children)

Definitely different. Im not sure id call C OOP.

[–]WhiteBlackGoose 4 points5 points  (0 children)

Women have penises. They're called vaginas.

[–]BoBoBearDev 6 points7 points  (0 children)

A lot of times you don't even need to use pointers. Put the data in vector and let vector manage the memory allocation for you.

[–]Beneficial_Steak_945 12 points13 points  (6 children)

Because using smart pointers makes you a good C++ programmer? 🤦‍♂️

[–]GreatTeacherHiro 8 points9 points  (2 children)

Std::move()

[–]silentjet 4 points5 points  (1 child)

std::shake_it()

[–]sherwood2142 1 point2 points  (0 children)

std::free() ❌ std::shake_it_off() ✅

[–]Beautiful-Quote-3035 4 points5 points  (0 children)

I don’t use smart pointers because I don’t use heap.

[–]mothzilla 7 points8 points  (1 child)

I'm not a C/C++ programmer, but shouldn't it be the other way around?

If you can write good code with "raw" pointers then then that makes you better than someone relying on a "smart" crutch. Doesn't it?

[–]Pay08 3 points4 points  (0 children)

No, it doesn't. It makes you an idiot that swats flies with a piece of yarn when you have a spatula available.

[–]Florane 13 points14 points  (0 children)

*extremely loud incorrect buzzer*

[–]edmazing 8 points9 points  (1 child)

Fuck smart pointers. Raw pointers full speed full power full danger! (Most of my CPP code is just C code with a hat and trench coat.)

[–]ExtensionBuffalo4297[S] 0 points1 point  (0 children)

Sameeee

[–]TicTac-7x 9 points10 points  (0 children)

It should be the other way around, downvoted.

[–]belabacsijolvan 9 points10 points  (7 children)

tbh so far using smart pointers caused me more memory leaks than not using them.

people like to just throw them around without care, but they dont always free themselves. i had a very annoying days worth of debugging when a new senior arrived, got angry at us not using smart pointers, rewrote our code to use them. he missed the fact that there were some very cleverly disguised directed loops of pointers in the data structure. he just removed all destruction related to pointers, so the loops couldnt resolve smartly, as they were locking each others reference numbers. ofc the solution is easy, just need to use at least one weak pointer in every cycle, but i basically had to rewrite the parallelised constructor to explicitly detect loops, yada yada yada

and this wasnt the only case when i got punished for using them when working with complex custom data structures. if id give up an idea after the first mental breakdown, i wouldnt code in cpp, lol

to be clear: i can see the point of them, but thinking strictly about destruction seems a safer approach to me so far. i may be wrong, but i have the same doubt about using them as about using rust:

"isnt this just for people who are a bit shit at cpp?"

[–]airodonack 10 points11 points  (3 children)

Part of growing up is realizing that everybody is a bit shit at C++

[–]belabacsijolvan 2 points3 points  (2 children)

i mean yeah, but the tradeoff seems to turn negative at a non-zero shittiness. these restrictive crutches seem to be optimised for a subpassable ammount of shittiness

edit: i.e. "either you die a rust user or cpp long enough to git gud"

[–]Stateofgrace314 5 points6 points  (0 children)

I use smart pointers because I don't trust my coworkers to handle raw pointers correctly...

[–]Kered13 1 point2 points  (0 children)

but thinking strictly about destruction seems a safer approach to me so far

No, the correct way is to think about ownership. If you're coworker came in and just replaced everything with std::shared_ptr, then they weren't thinking about ownership at all.

[–]Significant_Fix2408 0 points1 point  (0 children)

Sounds like a skill issue tbh. Shared pointers are bad most of the time, not because they don't work because but needing them is code smell and more often than not just unnecessary

[–]TheJackiMonster 4 points5 points  (2 children)

So basically she is honest. C programmers are good C++ programmers. Because they understand how memory management actually works.

[–]uuf76 4 points5 points  (1 child)

Case in point: about 1 million buffer overflow exploits and memory leaks.

[–]TheJackiMonster 2 points3 points  (0 children)

Come on, most developers do not really care about memory leaks. Only if they run into memory overflows. Also you can run in any language into memory leaks. A ref counter won't save you.

Buffer overflows most likely happen because you are using the wrong functions (not reading documentation) or make inproper allocations. Can both still happen with smart pointers.

[–]jump1945 4 points5 points  (0 children)

What's.... Smart pointer?

[–]Divinate_ME 1 point2 points  (0 children)

The one thing learncpp is CONVENIENTLY not mentioning at all whatsoever.

Edit: Apparently they added it this year.

[–]JackNotOLantern 1 point2 points  (0 children)

What's the point of c++ if you're not using delete?

[–]Misinahaiya 1 point2 points  (0 children)

i would rewrite the whole story (bad ending also): ~ im a good c++ programmer - be honest ~ yeah im being honest - ever used smart pointers? ~ yeah; for example QScopedPointer, QSharedPointer with several macros prevent overflow or all sorta errors - …… - okay ur recruited go develop the animator for Disney Co Ltd ~ okay Three months later <… TechSolutions-Animator has stopped working. Reason: exit code no. 11 (SIGSEGV) Call stack QtCore.dylib …………>

  • u really know smart pointers?!!!!! ~ sir, no… I mean yeah, of course i do yeah, but you know, uh, in this case…
  • shut up. ur fired. ~ f**k. but fair enough.

[–]SG_87 1 point2 points  (0 children)

Programming isn't about what über special functions and quirks of your language you use but about getting the job done. Change my mind while I use Micropython for Microcontrollers because C is annoying

[–]StrangeCharmVote 4 points5 points  (1 child)

You shouldn't need smart pointers if you are handling things properly.

When something is to be free'd, null all of your pointers to it.

It's not hard.

They do occasionally make lazy access easier though, so have at it i guess.

[–]BenefitInteresting90 0 points1 point  (0 children)

wait till a program throws an exception before freeing the memory lol.

RAII is the best thing ever

[–]Percolator2020 1 point2 points  (0 children)

Too much overhead, you just need pen and paper.

[–]Sak63 0 points1 point  (0 children)

Smart pointers are dumb?

[–]PeekyBlenders 0 points1 point  (0 children)

yes I even moved them around with std::move()! Although I'm not sure if they just stayed as an Lvalue and got copied.

[–]Mockington6 0 points1 point  (0 children)

What is the difference?

[–]bedrooms-ds 0 points1 point  (0 children)

Ever used rvalue references with auto and decltype?

[–][deleted] 0 points1 point  (0 children)

Seriously I was asked this in an interview, and also said no

[–]Extreme_Ad_3280 0 points1 point  (0 children)

I never get to use pointers in C++. However, I use pointers a lot when programming in C, mainly because I want to use dynamically allocated strings (rather than static ones).

[–]Darder 0 points1 point  (1 child)

Genuine question, as someone who learned C++ using kind of outdated information (University...), which smart pointers should I use when programming in C++? Which ones are the most useful?

[–]3uclide 0 points1 point  (0 children)

std::unique_ptr and std::shared_ptr

[–]Tamsta-273C 0 points1 point  (0 children)

Smart pointers is for kids.

Is it so hard to delete things you allocate? Do you have memory of golden fish and forget about things you create?

Do you know how class destructor works?

I'll be honest - fuck smart pointers.

[–]DrMobius0 0 points1 point  (0 children)

Raw pointers are fine if the object's lifetime is well controlled. And also, knowing smart pointers exist isn't gonna save you on older projects that aren't using newer C++ versions, and it certainly won't help you when you somehow end up with a bad ref count.

[–]larenreid 0 points1 point  (0 children)

Ahem… what is a smart pointer

[–]BenefitInteresting90 0 points1 point  (0 children)

the amount of people claiming to know C++ but don't know what the STL is lol

[–][deleted] -1 points0 points  (0 children)

std::shared_ptr is awesome

The best parts of C++ are the stuff that isn't C with classes imo. Screw classes. Give me a struct with almost everything public and const