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 memory

delete memory

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++data-structuresperformancetutorial
10 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.
  • M Offline
    M Offline
    Monark
    wrote on last edited by
    #1

    Hi All, I am new to c++ and facing one prob. I have one base class Vehicle and two sub class Car and Motor. I have an array of pointer to sub class but i m not able to delete the memory for that. CVehicle CCar : CVehicle CMotor : CVehicle CVehicle** myVehicle; myVehicle = new CVehicle*[3]; myVehicle[0] = new CCar; myVehicle[1] = new CMotor; myVehicle[2] = new CCar; now when I tried to reallocate the memroy, it is giving an error. delete myVehicle ; Any idea how to delete the memory?? Any help Monark - I am learning

    S T K N 4 Replies Last reply
    0
    • M Monark

      Hi All, I am new to c++ and facing one prob. I have one base class Vehicle and two sub class Car and Motor. I have an array of pointer to sub class but i m not able to delete the memory for that. CVehicle CCar : CVehicle CMotor : CVehicle CVehicle** myVehicle; myVehicle = new CVehicle*[3]; myVehicle[0] = new CCar; myVehicle[1] = new CMotor; myVehicle[2] = new CCar; now when I tried to reallocate the memroy, it is giving an error. delete myVehicle ; Any idea how to delete the memory?? Any help Monark - I am learning

      S Offline
      S Offline
      sw thi
      wrote on last edited by
      #2

      When you are deleting an array of elements that you created using new, you should be doing it using '[]' delete []variable;

      1 Reply Last reply
      0
      • M Monark

        Hi All, I am new to c++ and facing one prob. I have one base class Vehicle and two sub class Car and Motor. I have an array of pointer to sub class but i m not able to delete the memory for that. CVehicle CCar : CVehicle CMotor : CVehicle CVehicle** myVehicle; myVehicle = new CVehicle*[3]; myVehicle[0] = new CCar; myVehicle[1] = new CMotor; myVehicle[2] = new CCar; now when I tried to reallocate the memroy, it is giving an error. delete myVehicle ; Any idea how to delete the memory?? Any help Monark - I am learning

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

        when you use new, you must use delete. when you use new[], you must use delete[]. BTW, i see a problem in your design. a Car is actually a Vehicle, so, it's ok to derive it from CVehicle. but a Motor is not a Vehicle. a Vehicle HAS A Motor... it seems that you confused with the IS A relationship. also, are you sure to wanting to create an array of pointers rather than an array of objects ?


        [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

        1 Reply Last reply
        0
        • M Monark

          Hi All, I am new to c++ and facing one prob. I have one base class Vehicle and two sub class Car and Motor. I have an array of pointer to sub class but i m not able to delete the memory for that. CVehicle CCar : CVehicle CMotor : CVehicle CVehicle** myVehicle; myVehicle = new CVehicle*[3]; myVehicle[0] = new CCar; myVehicle[1] = new CMotor; myVehicle[2] = new CCar; now when I tried to reallocate the memroy, it is giving an error. delete myVehicle ; Any idea how to delete the memory?? Any help Monark - I am learning

          N Offline
          N Offline
          Nishad S
          wrote on last edited by
          #4

          You need to deallocate as delete myVehicle[0]; delete myVehicle[1]; delete myVehicle[2]; delete [] myVehicle;

          - NS -

          1 Reply Last reply
          0
          • M Monark

            Hi All, I am new to c++ and facing one prob. I have one base class Vehicle and two sub class Car and Motor. I have an array of pointer to sub class but i m not able to delete the memory for that. CVehicle CCar : CVehicle CMotor : CVehicle CVehicle** myVehicle; myVehicle = new CVehicle*[3]; myVehicle[0] = new CCar; myVehicle[1] = new CMotor; myVehicle[2] = new CCar; now when I tried to reallocate the memroy, it is giving an error. delete myVehicle ; Any idea how to delete the memory?? Any help Monark - I am learning

            K Offline
            K Offline
            KarstenK
            wrote on last edited by
            #5

            use the delete operator carefully. With delete you delete pointers: first delete myVehicle[0]; delete myVehicle[1]; delete myVehicle[2]; delete *myVehicle;//pointer to pointer Hammer in your head: to every new belongs a delete :-O

            Greetings from Germany

            D 1 Reply Last reply
            0
            • K KarstenK

              use the delete operator carefully. With delete you delete pointers: first delete myVehicle[0]; delete myVehicle[1]; delete myVehicle[2]; delete *myVehicle;//pointer to pointer Hammer in your head: to every new belongs a delete :-O

              Greetings from Germany

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

              KarstenK wrote:

              delete *myVehicle;//pointer to pointer

              Your syntax is a bit askew. Use:

              delete [] myPointer;


              "A good athlete is the result of a good and worthy opponent." - David Crow

              "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

              K 1 Reply Last reply
              0
              • D David Crow

                KarstenK wrote:

                delete *myVehicle;//pointer to pointer

                Your syntax is a bit askew. Use:

                delete [] myPointer;


                "A good athlete is the result of a good and worthy opponent." - David Crow

                "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                K Offline
                K Offline
                KarstenK
                wrote on last edited by
                #7

                I chose this way use the same syntax as the declararion: to make it easier readable

                Greetings from Germany

                D 1 Reply Last reply
                0
                • K KarstenK

                  I chose this way use the same syntax as the declararion: to make it easier readable

                  Greetings from Germany

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

                  KarstenK wrote:

                  I chose this way use the same syntax as the declararion: to make it easier readable

                  But it is wrong. As the address being deleted is not the same as the one allocated, you will receive an access violation.


                  "A good athlete is the result of a good and worthy opponent." - David Crow

                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                  K 1 Reply Last reply
                  0
                  • D David Crow

                    KarstenK wrote:

                    I chose this way use the same syntax as the declararion: to make it easier readable

                    But it is wrong. As the address being deleted is not the same as the one allocated, you will receive an access violation.


                    "A good athlete is the result of a good and worthy opponent." - David Crow

                    "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                    K Offline
                    K Offline
                    KarstenK
                    wrote on last edited by
                    #9

                    it was (not my code as followed) allocated: CVehicle** myVehicle; myVehicle = new CVehicle*[3]; so I would delete *myVehicle; or would I get (a deservred) exception :~

                    Greetings from Germany

                    D 1 Reply Last reply
                    0
                    • K KarstenK

                      it was (not my code as followed) allocated: CVehicle** myVehicle; myVehicle = new CVehicle*[3]; so I would delete *myVehicle; or would I get (a deservred) exception :~

                      Greetings from Germany

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

                      You're missing it entirely. myVehicle and *myVehicle point to two different addresses, with the latter already having been freed (via delete myVehicle[0]). Rather than guess at what you are trying to do, why not just try it?


                      "A good athlete is the result of a good and worthy opponent." - David Crow

                      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                      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