delete

Discussion in 'C++' started by Christian Meier, Aug 26, 2004.

  1. Hi NG

    I don't know if this problem is system dependend but I don't think so.
    So, here is my assumption: "delete" deletes a variable. "delete[]" deletes
    an array. Is this correct so far?
    And now check this code out:

    #include <iostream>
    using namespace std;

    int main()
    {
    char * p[100];
    for(int i = 0; i < 100; ++i) {
    p = new char[200000];
    } // for

    for(int i = 0; i < 100; ++i) {
    delete[] (p);
    //delete (p); <-- this does the same on Linux with g++
    } // for

    }


    p is pointing to an array, so why doesn't it matter whether I write
    "delete" with or without "[]"?
    Or is this only on my system?

    Thanks!

    Greets Chris
    Christian Meier, Aug 26, 2004
    #1
    1. Advertising

  2. Christian Meier

    Rolf Magnus Guest

    Christian Meier wrote:

    > Hi NG
    >
    > I don't know if this problem is system dependend but I don't think so.
    > So, here is my assumption: "delete" deletes a variable. "delete[]"
    > deletes an array. Is this correct so far?


    Not exactly (a variable is a named object, and dynamically allocated
    objects don't have names - and of course you only delete dynamically
    allocated objects), but I assume that by "a variable", you mean a
    single object. So yes, it's correct.

    > And now check this code out:
    >
    > #include <iostream>
    > using namespace std;
    >
    > int main()
    > {
    > char * p[100];
    > for(int i = 0; i < 100; ++i) {
    > p = new char[200000];
    > } // for
    >
    > for(int i = 0; i < 100; ++i) {
    > delete[] (p);
    > //delete (p); <-- this does the same on Linux with g++
    > } // for
    >
    > }
    >
    >
    > p is pointing to an array, so why doesn't it matter whether I write
    > "delete" with or without "[]"?
    > Or is this only on my system?


    This might be system specific. Some systms will let you go without "[]",
    but it's still not correct.
    Rolf Magnus, Aug 26, 2004
    #2
    1. Advertising

  3. Christian Meier

    Ali Cehreli Guest

    On Thu, 26 Aug 2004 10:55:49 -0700, Christian Meier wrote:

    > So, here is my assumption: "delete" deletes a variable. "delete[]"
    > deletes an array. Is this correct so far?


    Deleting includes calling destructors. Since fundamental types have
    no-op destructors, you don't see any problem below.

    > for(int i = 0; i < 100; ++i) {
    > p = new char[200000];


    > for(int i = 0; i < 100; ++i) {
    > delete[] (p);
    > //delete (p); <-- this does the same on Linux with g++


    That is undefined behavior. Anything can happen... At least, the
    destructors wouldn't be called if you used more complex types.

    > p is pointing to an array, so why doesn't it matter whether I write
    > "delete" with or without "[]"?
    > Or is this only on my system?


    Unfortunately it gives you the impression that it works, but it is
    undefined behavior.

    Ali
    Ali Cehreli, Aug 26, 2004
    #3
  4. Christian Meier wrote:

    > Hi NG
    >
    > I don't know if this problem is system dependend but I don't think so.
    > So, here is my assumption: "delete" deletes a variable. "delete[]" deletes
    > an array. Is this correct so far?
    > And now check this code out:
    >
    > #include <iostream>
    > using namespace std;
    >
    > int main()
    > {
    > char * p[100];
    > for(int i = 0; i < 100; ++i) {
    > p = new char[200000];
    > } // for
    >
    > for(int i = 0; i < 100; ++i) {
    > delete[] (p);
    > //delete (p); <-- this does the same on Linux with g++
    > } // for
    >
    > }
    >
    >
    > p is pointing to an array, so why doesn't it matter whether I write
    > "delete" with or without "[]"?


    This can be clarified by having a much simpler example.

    char * p = new char[200000];
    // Do something with p
    delete [] p;

    > Or is this only on my system?


    This is undefined behaviour - meaning anything can happen.

    Depending upon the implementation you are using, you can also think of
    memory profilers to make sure that your program does not have any memory
    leaks.

    electricFence - on GNU/Linux just to name one.
    mtrace utility on GNU/Linux .
    These relevant utilities should help you understand if there is a
    leak in your code.

    --
    Karthik.
    Karthiik Kumar, Aug 28, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sandeep Grover

    delete on delete !

    Sandeep Grover, Jul 12, 2003, in forum: C++
    Replies:
    19
    Views:
    616
    Chris \( Val \)
    Jul 22, 2003
  2. HeroOfSpielburg
    Replies:
    1
    Views:
    387
    Alf P. Steinbach
    Aug 6, 2003
  3. 0to60
    Replies:
    4
    Views:
    311
    Jerry Coffin
    Dec 19, 2003
  4. Mathieu Malaterre

    delete NULL, what about delete[] NULL

    Mathieu Malaterre, Aug 17, 2004, in forum: C++
    Replies:
    2
    Views:
    3,821
    Mathieu Malaterre
    Aug 17, 2004
  5. Jef Driesen
    Replies:
    1
    Views:
    500
    Gernot Frisch
    Jan 19, 2005
Loading...

Share This Page