delete [ ] fp qn

Discussion in 'C++' started by mescaline, Dec 29, 2003.

  1. mescaline

    mescaline Guest

    Hi,

    suppose I do:

    class One{};

    int main(){
    One * A = new One[100];
    // an array of objects on the heap ...
    delete A; // instead of delete []A;
    return 0;
    }

    .... I read in Eckel, that "this releases the proper amount of storage"
    -- How?

    Also, then there is no memory leak, right?
    What are the disadvantages of doing the above then instead of "delete []A;"?

    thanks
    m
     
    mescaline, Dec 29, 2003
    #1
    1. Advertising

  2. mescaline

    Jeff Schwab Guest

    mescaline wrote:
    > Hi,
    >
    > suppose I do:
    >
    > class One{};
    >
    > int main(){
    > One * A = new One[100];
    > // an array of objects on the heap ...
    > delete A; // instead of delete []A;
    > return 0;
    > }
    >
    > ... I read in Eckel, that "this releases the proper amount of storage"
    > -- How?
    >
    > Also, then there is no memory leak, right?
    > What are the disadvantages of doing the above then instead of "delete []A;"?
    >
    > thanks
    > m



    Use delete[]. I don't know why Eckel would say differently.
     
    Jeff Schwab, Dec 29, 2003
    #2
    1. Advertising

  3. mescaline

    Ron Natalie Guest


    > ... I read in Eckel, that "this releases the proper amount of storage"
    > -- How?
    >
    > Also, then there is no memory leak, right?
    > What are the disadvantages of doing the above then instead of "delete []A;"?
    >

    The disadvantage is that it is undefined behavior. If Eckel said that, he's wrong.
    There's no guarantee that anything specific happens. It's quite possible you'll
    hose the allocators.
     
    Ron Natalie, Dec 29, 2003
    #3
  4. On Mon, 29 Dec 2003 06:44:36 -0800, mescaline wrote:

    > Hi,
    >
    > suppose I do:
    >
    > class One{};
    >
    > int main(){
    > One * A = new One[100];
    > // an array of objects on the heap ...
    > delete A; // instead of delete []A;
    > return 0;
    > }
    >
    > ... I read in Eckel, that "this releases the proper amount of storage"
    > -- How?


    I haven't read Eckel, but given how highly regarded he is, I doubt he
    wrote that, or not in this context.

    > Also, then there is no memory leak, right? What are the disadvantages of
    > doing the above then instead of "delete []A;"?


    It invokes undefined behaviour. Anything may happen. Anything may even
    include works, seems to work but it might also blow up.

    HTH,
    M4
     
    Martijn Lievaart, Dec 29, 2003
    #4
  5. mescaline

    Unforgiven Guest

    mescaline wrote:
    > Hi,
    >
    > suppose I do:
    >
    > class One{};
    >
    > int main(){
    > One * A = new One[100];
    > // an array of objects on the heap ...
    > delete A; // instead of delete []A;
    > return 0;
    > }
    >
    > ... I read in Eckel, that "this releases the proper amount of storage"
    > -- How?
    >
    > Also, then there is no memory leak, right?
    > What are the disadvantages of doing the above then instead of "delete
    > []A;"?


    If you say:
    One *B = new One;
    Memory is allocated and the constructor is called.
    If you say:
    One *A = new One[100];
    Memory is allocated and 100 constructors are called.

    Similarly if you say:
    delete[] A;
    All 100 destructors are called and memory is deallocated
    But if you say:
    delete A;
    Only 1 destructor is called and memory is deallocated. While at the very
    least all compilers I know (I'm not sure if the standard has anything to say
    about this) will deallocate all memory, only the first of the in this case
    100 objects is properly destructed. If the other 99 objects manage any
    resources (like memory) that rely on the destructor to be released, those
    will leak if you don't use delete[].

    --
    Unforgiven
    "Most people make generalisations"
    Freek de Jonge
     
    Unforgiven, Dec 29, 2003
    #5
  6. mescaline

    Ron Natalie Guest

    "Unforgiven" <> wrote in message news:bspjoi$f2sse$-berlin.de...

    > But if you say:
    > delete A;
    > Only 1 destructor is called and memory is deallocated.


    Nope, it's undefined behavior. It's quite possible that the memory
    doesn't get deallocated properly either.

    It's worse than a leak.
     
    Ron Natalie, Dec 29, 2003
    #6
    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:
    640
    Chris \( Val \)
    Jul 22, 2003
  2. HeroOfSpielburg
    Replies:
    1
    Views:
    399
    Alf P. Steinbach
    Aug 6, 2003
  3. 0to60
    Replies:
    4
    Views:
    320
    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,851
    Mathieu Malaterre
    Aug 17, 2004
  5. Jef Driesen
    Replies:
    1
    Views:
    513
    Gernot Frisch
    Jan 19, 2005
Loading...

Share This Page