Pointer - Protection from deletion
-
-
Is there anyway I can protect a pointer from being deleted by a function, but at the same time allow the function to operate on the pointer? In other words, I want to protect the pointer from accidentally being deleted by the consuming function.
How can a pointer accidentally be deleted? Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
How can a pointer accidentally be deleted? Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
I didnt mean 'accidentally deleted' in that way, what i meant was that, if i pass a pointer to a function, i dont want the function to take 'ownership' and delete it etc (due to bad programming on the function side, which i have no control of it). If I allocated the pointer, only i will have the ability to delete it. Other consuming functions can only process, read/write and not delete. Can it be done?
-
I didnt mean 'accidentally deleted' in that way, what i meant was that, if i pass a pointer to a function, i dont want the function to take 'ownership' and delete it etc (due to bad programming on the function side, which i have no control of it). If I allocated the pointer, only i will have the ability to delete it. Other consuming functions can only process, read/write and not delete. Can it be done?
uus831 wrote:
Can it be done?
Not without control of the consuming function. The caller wouldn't even know if the pointer was deleted by the consumer.
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
uus831 wrote:
Can it be done?
Not without control of the consuming function. The caller wouldn't even know if the pointer was deleted by the consumer.
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Short Answer: NO. Longer Answer: You seem to be used to VB. Welcome to 'C' One of the Advantages of 'C' and 'CPP' over the likes of Basic is, that you have access at your own discression to your own dicression to all actual bytes in memory. That means, Greater Access, but also No Secret Actions which change the world around you when you're not looking. This great power comes with the great responsibility of having to put into place your own system of checks and balances to ensure that you don't use a pointer after you delete it's contents. The Standard way of dealing with this is a programming discipline of setting a pointer to NULL when de-allocated, and of duplicating a pointer only under reserve. This is a very big subject in terms of language definition, but quite simple if you get a picture in mind about how code is laid out. Regards :)
Bram van Kampen
-
Short Answer: NO. Longer Answer: You seem to be used to VB. Welcome to 'C' One of the Advantages of 'C' and 'CPP' over the likes of Basic is, that you have access at your own discression to your own dicression to all actual bytes in memory. That means, Greater Access, but also No Secret Actions which change the world around you when you're not looking. This great power comes with the great responsibility of having to put into place your own system of checks and balances to ensure that you don't use a pointer after you delete it's contents. The Standard way of dealing with this is a programming discipline of setting a pointer to NULL when de-allocated, and of duplicating a pointer only under reserve. This is a very big subject in terms of language definition, but quite simple if you get a picture in mind about how code is laid out. Regards :)
Bram van Kampen
-
uus831 wrote:
Have to trust the programmer
Yeah. The best you can do is have a well defined, consistent, and documented interface. It's still up to us programmers to follow the rules, no different than using something like the Win32 APIs. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java: