Click here to Skip to main content
15,946,529 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
RantSmart pointers Pin
Pascal Ganaye15-Nov-11 6:15
Pascal Ganaye15-Nov-11 6:15 
I'd like to speak about pointers, I am trying to stay neutral and thoughtful, please don't transform this thread into a language or belief war.

Pointers have several problems.
1 - they can be freed and they then point to garbage
2 - they can be freed twice
3 - sometime the programmer forget to free some memory
4 - probably more...

The most common solution these days seem to be using garbage collectors.
Garbage collector fix the issues 1,2, 3 above
Unfortunately garbage collectors has come with some new problems:
- you can't really release memory precisely when you want to
- it has to freeze the computer for some time occasionally to count all its blocks
- more...

There has been a lot of middle way attempts.
I know there are many flavour of smart pointers around, I am not claiming I know any of them and this is why I am writing here.

Lets imagine we have a language between C++ and C#

In this language :
- every allocation requires an owner.
The root owner would be the application itself.
- free is available and work synchronously
- when an object get freed, any child object is freed.
- when an object is freed any other pointer to it get nullified

This would solve our 3 original problems:

1 - they can be freed and they then point to garbage
They would point to null

2 - they can be freed twice
They would be freed once and then the pointer being null they would not be freed again

3 - sometime the programmer forget to free some memory
When the owner is freed, the object is freed

I was thinking of implementing those pointers and trying them on a free project like Apache or something like that.

But first, I thought I would submit it to all, there is a good chance that it already exists ...
Or perhaps the constraint of having an owner is too constraining.
It is hard to say until you try on a real project.

Having an owner per object could have other advantages in a multi-threading environment.
I haven't finished on this but first, does anyone have come across anything like it?
GeneralRe: Smart pointers Pin
Erudite_Eric15-Nov-11 7:33
Erudite_Eric15-Nov-11 7:33 
GeneralRe: Smart pointers Pin
JackDingler15-Nov-11 11:23
JackDingler15-Nov-11 11:23 
GeneralRe: Smart pointers Pin
Stefan_Lang17-Nov-11 4:42
Stefan_Lang17-Nov-11 4:42 
GeneralRe: Smart pointers Pin
JackDingler17-Nov-11 5:48
JackDingler17-Nov-11 5:48 
GeneralRe: Smart pointers Pin
Stefan_Lang17-Nov-11 5:56
Stefan_Lang17-Nov-11 5:56 
GeneralRe: Smart pointers Pin
Orjan Westin15-Nov-11 23:41
professionalOrjan Westin15-Nov-11 23:41 
GeneralRe: Smart pointers Pin
Erudite_Eric16-Nov-11 22:39
Erudite_Eric16-Nov-11 22:39 
GeneralRe: Smart pointers Pin
Orjan Westin21-Nov-11 4:33
professionalOrjan Westin21-Nov-11 4:33 
GeneralRe: Smart pointers Pin
Erudite_Eric21-Nov-11 5:59
Erudite_Eric21-Nov-11 5:59 
GeneralRe: Smart pointers Pin
Orjan Westin21-Nov-11 7:38
professionalOrjan Westin21-Nov-11 7:38 
GeneralRe: Smart pointers Pin
Stefan_Lang17-Nov-11 4:58
Stefan_Lang17-Nov-11 4:58 
GeneralRe: Smart pointers Pin
Orjan Westin21-Nov-11 4:35
professionalOrjan Westin21-Nov-11 4:35 
GeneralRe: Smart pointers Pin
Maximilien15-Nov-11 9:55
Maximilien15-Nov-11 9:55 
GeneralRe: Smart pointers Pin
JackDingler16-Nov-11 9:08
JackDingler16-Nov-11 9:08 
GeneralRe: Smart pointers Pin
Chris Losinger15-Nov-11 10:25
professionalChris Losinger15-Nov-11 10:25 
GeneralRe: Smart pointers Pin
Rick York16-Nov-11 13:30
mveRick York16-Nov-11 13:30 
GeneralRe: Smart pointers Pin
Chris Losinger16-Nov-11 13:47
professionalChris Losinger16-Nov-11 13:47 
GeneralRe: Smart pointers Pin
JackDingler15-Nov-11 11:26
JackDingler15-Nov-11 11:26 
GeneralRe: Smart pointers Pin
Pascal Ganaye15-Nov-11 11:32
Pascal Ganaye15-Nov-11 11:32 
GeneralRe: Smart pointers Pin
JackDingler15-Nov-11 11:42
JackDingler15-Nov-11 11:42 
GeneralRe: Smart pointers Pin
Pascal Ganaye15-Nov-11 11:46
Pascal Ganaye15-Nov-11 11:46 
GeneralRe: Smart pointers Pin
JackDingler15-Nov-11 12:01
JackDingler15-Nov-11 12:01 
GeneralRe: Smart pointers Pin
Pascal Ganaye15-Nov-11 21:52
Pascal Ganaye15-Nov-11 21:52 
GeneralRe: Smart pointers Pin
JackDingler16-Nov-11 2:04
JackDingler16-Nov-11 2:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.