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. Delete object, necessaray??

Delete object, necessaray??

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
12 Posts 6 Posters 0 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.
  • T tpndtbk

    Hi all, Is it necessary to delete an object by calling delete object; or the object will be automatically deleted if there's no variable assigned to it? Thanks in advance for your help.

    V Offline
    V Offline
    V 0
    wrote on last edited by
    #3

    if you use new you call delete ALWAYS. Even better would be to do this: Object *x = new Object(); //... do things with x delete x; x = NULL; This way you do not accidentally call x that points to something else than Object. (because the memory to which the pointer points to is overwritten) good luck. "If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix

    T 1 Reply Last reply
    0
    • T tpndtbk

      Hi all, Is it necessary to delete an object by calling delete object; or the object will be automatically deleted if there's no variable assigned to it? Thanks in advance for your help.

      T Offline
      T Offline
      tpndtbk
      wrote on last edited by
      #4

      Thank you for your help. Another related question.. if I have the class: class ObjectA { ObjectB b; } ObjectA a = new objectA(); When i detele ObjectA (delete a), do I have to delete ObjectB (delete b)? or is it implicitly deleted?

      T J D 3 Replies Last reply
      0
      • T tpndtbk

        Thank you for your help. Another related question.. if I have the class: class ObjectA { ObjectB b; } ObjectA a = new objectA(); When i detele ObjectA (delete a), do I have to delete ObjectB (delete b)? or is it implicitly deleted?

        T Offline
        T Offline
        tpndtbk
        wrote on last edited by
        #5

        And in the following case: I have ObjectA a = new ObjectA(); //... Do I have to delete a (delete a) before I create a new ObjectA and assign it to a?? a = new ObjectA();

        1 Reply Last reply
        0
        • T tpndtbk

          Thank you for your help. Another related question.. if I have the class: class ObjectA { ObjectB b; } ObjectA a = new objectA(); When i detele ObjectA (delete a), do I have to delete ObjectB (delete b)? or is it implicitly deleted?

          J Offline
          J Offline
          Jon Hulatt
          wrote on last edited by
          #6

          It seems like you need to do some reading on memory allocation in C / C++ When you declare an object like this:- ObjectA theObject; It is created for you, and it will be removed automatically when it goes out of scope. When you declare an object like this:- ObjectA *thePointerToObject = new ObjectA(); You are doing three things:- creating a pointer (which will be removed for you), creating a new instance of ObjectA (which it is your duty to clean up), and setting your new pointer to point to the new object. This really is basic C++. *Any* C++ tutorial book/site will explain this; go out there are read up!

          using System.Beer;

          1 Reply Last reply
          0
          • V V 0

            if you use new you call delete ALWAYS. Even better would be to do this: Object *x = new Object(); //... do things with x delete x; x = NULL; This way you do not accidentally call x that points to something else than Object. (because the memory to which the pointer points to is overwritten) good luck. "If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix

            T Offline
            T Offline
            toxcct
            wrote on last edited by
            #7

            V. wrote: if you use new you call delete ALWAYS. wrong. Windows can do it itself, but that's a very bad way to program...


            TOXCCT >>> GEII power
            [toxcct][VisualCalc]

            V D 2 Replies Last reply
            0
            • T tpndtbk

              Hi all, Is it necessary to delete an object by calling delete object; or the object will be automatically deleted if there's no variable assigned to it? Thanks in advance for your help.

              T Offline
              T Offline
              toxcct
              wrote on last edited by
              #8

              you should never wonder if the system will perform such operation for you. if you own the object, then take use to delete it when you don't need it anymore. if one day you come to work on poor operating systems which don't do this for you, or worse, systems that don't have any OS, you will have serious lacks of memory !!! think of it...


              TOXCCT >>> GEII power
              [toxcct][VisualCalc]

              1 Reply Last reply
              0
              • T toxcct

                V. wrote: if you use new you call delete ALWAYS. wrong. Windows can do it itself, but that's a very bad way to program...


                TOXCCT >>> GEII power
                [toxcct][VisualCalc]

                V Offline
                V Offline
                V 0
                wrote on last edited by
                #9

                ok I correct myself: If you're a good programmerand if you use new you call delete ALWAYS :-D "If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix

                T 1 Reply Last reply
                0
                • V V 0

                  ok I correct myself: If you're a good programmerand if you use new you call delete ALWAYS :-D "If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix

                  T Offline
                  T Offline
                  toxcct
                  wrote on last edited by
                  #10

                  much better ! :-D:cool::rose:


                  TOXCCT >>> GEII power
                  [toxcct][VisualCalc]

                  1 Reply Last reply
                  0
                  • T toxcct

                    V. wrote: if you use new you call delete ALWAYS. wrong. Windows can do it itself, but that's a very bad way to program...


                    TOXCCT >>> GEII power
                    [toxcct][VisualCalc]

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #11

                    toxcct wrote: Windows can do it itself... Only if the application exits or is terminated. Things like services are designed to run for long periods of time so a call to delete is a requirement.


                    "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                    1 Reply Last reply
                    0
                    • T tpndtbk

                      Thank you for your help. Another related question.. if I have the class: class ObjectA { ObjectB b; } ObjectA a = new objectA(); When i detele ObjectA (delete a), do I have to delete ObjectB (delete b)? or is it implicitly deleted?

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #12

                      Stack-based variables, as opposed to heap-based variables (i.e., those created with new), are cleaned up when the variable goes out of scope.


                      "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                      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