Deleting a variable sometimes allocated with new

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TamusJRoyce
    New Member
    • Apr 2008
    • 108

    Deleting a variable sometimes allocated with new

    I've seen that it is valid to deallocate any variable using delete, such as:
    # int numVar = 5; delete numVar;
    and numVar no longer exists (which is the same as letting a variable go out of scope).

    I'm wanting to do some lazy de-allocation where I can delete a variable located at a pointer which is sometimes allocated by new, but not always (right before my program closes, so no chance of needing that variable anyways).

    Rather than keep track of each individual variable if it has been allocated dynamically, I am curious if this works:

    Code:
    #include <math.h>
    
    int *numVarPtr, numVar = 5;
    if (rand() % 2) {
        numVarPtr = new int;
        *numVarPtr = 6;
    } else {
        numVarPtr = &numVar;
    } // End if
    
    delete *numVarPtr;
    Would it be valid within C++ to do this? I am concerned with this being valid based on standards more than if it happens to work on one or two compilers...
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    It is not valid to use delete on what a pointer references to. It is only valid to use delete on a pointer:

    [code=c]int *intpointer = new int;
    delete intpointer; // Legal
    delete *intpointer; // Huh? You can't delete an int, only an int* (or other pointers)[/code]

    I'm not sure if you can delete a pointer when it has been assigned to the address of a pre-existing, stack variable, but it doesn't seem very useful.

    Comment

    • TamusJRoyce
      New Member
      • Apr 2008
      • 108

      #3
      Darn... Thanks for noticing the mistake in my code Ganon11. It should have been:

      Code:
          #include <math.h>
         int *numVarPtr, numVar = 5;
         if (rand() % 2) {
         numVarPtr = new int;
         *numVarPtr = 6;
         } else {
         numVarPtr = &numVar;
         } // End if
         delete numVarPtr;
      The code I'm working on is a bit more complex than the above, and it slipped by me.

      And if you are curious to what I'm trying to apply this example to, I'm making a neural network in which each axon struct will point to a neuron variable. This variable may be non-dynamically set within the program, or dynamically allocated. But for simplicity sake, I'm using the example above...

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        You should only use delete on a block of memory allocated using new. Anything else at best is going to be undefined behaviour and is quite likely to immediately cause a seg fault.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          <Puts on official admin hat>
          That other thread you started referring to this thread is strictly speaking a double post.

          The correct thing to do would have been to repost to this thread to try and grab our attention again.

          I have now removed the other thread, please try not to double post in future.
          </hat>

          Comment

          • TamusJRoyce
            New Member
            • Apr 2008
            • 108

            #6
            Thank you very much banfa. I rely on moderators way too much. I'll be sure not to do that again.

            Things would be so much simpler if I could delete any memory I wanted. Oh well. I guess I have to take the long route : )

            I rarely need a quick answer, but I need my neural net done in a few days for reference to employers who I'm trying to get a career with. Thanks for your reply, and for telling me how to be "re-noticed".

            *bows down to the awesomeness of your admin hat

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by TamusJRoyce
              Things would be so much simpler if I could delete any memory I wanted. Oh well. I guess I have to take the long route : )
              That route isn't that long. You can only delete/free objects if you have new'd/malloc'd
              them. Local objects (local to functions) just disappear when the function terminates.
              Global objects exist during the entire life span of the process.

              If you think about it: how could you delete/free an object that is stored on the stack?
              Of course you can 'invalidate' them but that is just a logical action; physically the
              space taken by the object is still there. Ditto for global objects, i.e. you can't
              shoot holes in a consecutive sequence of bytes.

              kind regards,

              Jos

              Comment

              • TamusJRoyce
                New Member
                • Apr 2008
                • 108

                #8
                I guess it would be pretty OS dependent to be able to ask where in memory the variable exists, the program's stack or the heap, and delete it based on that.

                I see how bad this question is now anyways. Even if I considered all variables to be allocated with new, and delete a local variable myself, that local variable will inevitably be deleted within the program a second time.

                Comment

                Working...