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 and delete [] what is the difference

delete and delete [] what is the difference

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 Posts 5 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    what is the difference betweeen delete and delete []

    M 1 Reply Last reply
    0
    • L Lost User

      what is the difference betweeen delete and delete []

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      If you allocate an array with new, you use delete [] when you free the memory. --Mike-- http://home.inreach.com/mdunn/ This must be Thursday. I never could get the hang of Thursdays...

      E 1 Reply Last reply
      0
      • M Michael Dunn

        If you allocate an array with new, you use delete [] when you free the memory. --Mike-- http://home.inreach.com/mdunn/ This must be Thursday. I never could get the hang of Thursdays...

        E Offline
        E Offline
        Etienne Danvoye
        wrote on last edited by
        #3

        But delete and delete[] both seem to generate the same assembly output, so is there really a difference ? :confused:

        E R 2 Replies Last reply
        0
        • E Etienne Danvoye

          But delete and delete[] both seem to generate the same assembly output, so is there really a difference ? :confused:

          E Offline
          E Offline
          Erik Funkenbusch
          wrote on last edited by
          #4

          They may look the same, but they're not. For instance, take this simple application:

          #include "stdafx.h"
          #include
          using namespace std;
          class a
          {
          public:
          a(){};
          ~a(){cout << "a::~a()\n";};
          };

          int main(int argc, char* argv[])
          {
          a *pa = new a[10];
          delete [] pa;
          cout << "------" << endl;
          pa = new a[10];
          delete pa;

          return 0;
          

          }

          If your theory were correct, it would output 10 a::~a()'s followed by dashes and 10 more. Instead, this is the output:

          a::~a()
          a::~a()
          a::~a()
          a::~a()
          a::~a()
          a::~a()
          a::~a()
          a::~a()
          a::~a()
          a::~a()

          a::~a()

          As you can see, the destructor gets called 10 times when using delete [] and only once when using delete.

          1 Reply Last reply
          0
          • E Etienne Danvoye

            But delete and delete[] both seem to generate the same assembly output, so is there really a difference ? :confused:

            R Offline
            R Offline
            Ryan Park
            wrote on last edited by
            #5

            Delete[] is necessary for deletion of objects. I mean instances of classes. Objects in array should be garanteed to destruct itself and free resource. Delete[] calls all of destructor of objects in array and call of destructor calls destructor of base class's destructor. So delete[] gaurantees destruction of all object in array . For primitive types like int, long, char etc.. You don't have use delete[] for freeing resource. Delete is enough. But this is only the theory.. In practice,I recommend to use delete[] whenever you're freeing resource in array. Cause it makes code more clear and readable. It looks to clear to other developers who are seeing your code or youself in future. Regards, Ryan

            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