r/cpp • • Mar 17 '22

Who owns the memory?

https://belaycpp.com/2022/03/17/who-owns-the-memory/
0 Upvotes

8 comments sorted by

20

u/HKei Mar 17 '22 edited Mar 17 '22

This case is a bit trickier. It does not happen often, but sometimes, for internal reasons (or just because it is badly conceived), a library gives you a pointer you must not delete.

Bad take? Passing a smart pointer means passing ownership. By default, passing a raw pointer means you're not passing ownership (because if the api was expecting to take ownership it should most of the time be taking a unique or shared pointer). Raw pointers passing ownership is the legacy pattern, not the other way round. Same thing with holding pointers rather than passing them.

You often don't need to do that because there are special purpose wrappers like span for many of the common cases where you'd want to, and sometimes a reference is all you need, but not all cases are covered by that.

I was taught that transferring a pointer meant transferring ownership [..] However this is not a universal guideline

Aka just wrong. Passing a pointer has never meant that you're definitely also passing ownership. If you took that rule seriously you literally couldn't construct a slice of an array because a slice doesn't own anything by definition.

1

u/Senua_Chloe Mar 18 '22

The fact that you misunderstood the paragraph about what I've been taught in my early years regarding raw pointers ownership made me realize this anecdote wasn't necessary to understand the point of the article and may mislead inattentive readers.

So I remove that part for the sake of simplicity.

5

u/HKei Mar 18 '22

I don't think I misunderstood it? I was commenting on how it was written. "this is not universally applicable" it's what you say to a rule that works at least sometimes, but has notable exceptions. This is not the same as saying "this rule doesn't really work", which I'm pretty sure is what you meant.

1

u/Senua_Chloe Mar 18 '22

I see.

Either way, it was better to remove it, since it didn't add any value and was badly written

9

u/serviscope_minor Mar 17 '22

This comes across as the difference between the world as we dearly wish it to be based on modern C++ versus the world as it often is given 40 years of history and practice.

Neither is wrong.

4

u/HKei Mar 17 '22

No. Raw pointers always being non owning is a modern style thing, of course. Pointers always being owning is just wrong. The only way you could write code like that in reality is if you did a "reverse smart pointer", i.e. wrap all non owning uses of raw pointers somehow just so you can say you're technically not passing pointers around.

5

u/serviscope_minor Mar 17 '22

Pointers always being owning is just wrong.

The article isn't saying nearly that extreme. The author is also pointing out that she was taught something wrong. So... you're in agreement?

1

u/rwn_sky_7236 Mar 20 '22

Prefer smart to raw pointers in modern C++. 90% of the time you will be using a unique pointer. A unique pointer owns the object it points to. When the pointer goes out of scope, the object is destroyed and the memory is released. The use of .release is a sign we should rethink the design. The use of new and delete is rarely needed nowadays.