Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Pointer - Protection from deletion

Pointer - Protection from deletion

Scheduled Pinned Locked Moved C / C++ / MFC
question
8 Posts 3 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    uusheikh
    wrote on last edited by
    #1

    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.

    M 1 Reply Last reply
    0
    • U uusheikh

      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.

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      How can a pointer accidentally be deleted? Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      U 1 Reply Last reply
      0
      • M Mark Salsbery

        How can a pointer accidentally be deleted? Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        U Offline
        U Offline
        uusheikh
        wrote on last edited by
        #3

        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?

        M 1 Reply Last reply
        0
        • U uusheikh

          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?

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          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:

          U 1 Reply Last reply
          0
          • M Mark Salsbery

            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:

            U Offline
            U Offline
            uusheikh
            wrote on last edited by
            #5

            I'm looking at smart pointers, auto pointers. Can it be used?

            B 1 Reply Last reply
            0
            • U uusheikh

              I'm looking at smart pointers, auto pointers. Can it be used?

              B Offline
              B Offline
              Bram van Kampen
              wrote on last edited by
              #6

              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

              U 1 Reply Last reply
              0
              • B 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

                U Offline
                U Offline
                uusheikh
                wrote on last edited by
                #7

                Thanks.. I get the picture now. There is simply no way.. Have to trust the programmer :)

                M 1 Reply Last reply
                0
                • U uusheikh

                  Thanks.. I get the picture now. There is simply no way.. Have to trust the programmer :)

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #8

                  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:

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups