Question on delete [] vs just plain delete

Discussion in 'C++' started by DamonChong, Jan 29, 2005.

  1. DamonChong

    DamonChong Guest

    Hi,

    I am new to c++. I recently spend an enormous among of time
    troubleshooting a seeminly innocuous piece of code. Although I narrow
    down this piece of code as the culprit but I don't understand why. Can
    some guru help to enlighten me? Thank you.

    // I created an array of pointers to object pointers:

    Object ** obs = new Object * [9];

    // After that I populate this array:

    for(int i=0; i<9; i++){
    obs = new Object(i);
    }

    // After using them i deleted the array:

    for(int i=0; i<9; i++){
    delete obs;
    }

    delete [] obs;

    delete obs; // this is the problem, during execution the program
    hanged.

    Apparently, the above caused undefined behaviour. But I don't know why
    i shouldn't do that. Thanks again for any sharing!

    Cheers,
    Damon
     
    DamonChong, Jan 29, 2005
    #1
    1. Advertising

  2. DamonChong

    Efrat Regev Guest

    "DamonChong" <> wrote in message
    news:...
    > Hi,
    >
    > I am new to c++. I recently spend an enormous among of time
    > troubleshooting a seeminly innocuous piece of code. Although I narrow
    > down this piece of code as the culprit but I don't understand why. Can
    > some guru help to enlighten me? Thank you.


    While the gurus answer more difficult questions, I'll field this one.

    >
    > // I created an array of pointers to object pointers:
    >
    > Object ** obs = new Object * [9];


    /////////// Great, you've allocated an array of pointers to Object

    >
    > // After that I populate this array:
    >
    > for(int i=0; i<9; i++){
    > obs = new Object(i);


    /////////// And now you've allocated the objects.
    > }
    >
    > // After using them i deleted the array:
    >
    > for(int i=0; i<9; i++){
    > delete obs;


    /////////// Now you've deallocated the objects.

    > }
    >
    > delete [] obs;


    /////////// And now you've deallocated the array.


    /////////// Good. So you've deallocated everything you've allocated. Nothing
    left to deallocate.

    /////////// and yet....
    >
    > delete obs; // this is the problem, during execution the program
    > hanged.


    /////////// So the previous line hangs since you're deallocating something
    which you shouldn't
     
    Efrat Regev, Jan 29, 2005
    #2
    1. Advertising

  3. Hi Damon

    Efrat already answered your post, but I will recommend you do not use
    new/delete as a newbie C++ programmer. Instead focus on the standard library
    and just don't use pointers.
    Pointers are difficult and better not used until you understand the basics
    of C++ - including the standard library. By going this route, you will also
    learn how little use there actually is of pointers in modern C++.

    /Peter
    "DamonChong" <> skrev i en meddelelse
    news:...
    > Hi,
    >
    > I am new to c++. I recently spend an enormous among of time
    > troubleshooting a seeminly innocuous piece of code. Although I narrow
    > down this piece of code as the culprit but I don't understand why. Can
    > some guru help to enlighten me? Thank you.
    >
    > // I created an array of pointers to object pointers:
    >
    > Object ** obs = new Object * [9];
    >
    > // After that I populate this array:
    >
    > for(int i=0; i<9; i++){
    > obs = new Object(i);
    > }
    >
    > // After using them i deleted the array:
    >
    > for(int i=0; i<9; i++){
    > delete obs;
    > }
    >
    > delete [] obs;
    >
    > delete obs; // this is the problem, during execution the program
    > hanged.
    >
    > Apparently, the above caused undefined behaviour. But I don't know why
    > i shouldn't do that. Thanks again for any sharing!
    >
    > Cheers,
    > Damon
    >
     
    Peter Koch Larsen, Jan 29, 2005
    #3
  4. DamonChong

    ajk Guest

    On 29 Jan 2005 01:39:32 -0800, "DamonChong" <>
    wrote:

    >delete [] obs;
    >
    >delete obs; // this is the problem, during execution the program



    normally you use delete [] on any array you allocate to tell the
    compiler that the ptr you are deleting is an array.

    it is just one of those not obvious c++ rules you need to learn :)

    in any case I would suggest you use vector<> instead of array since it
    is safer and more convenient. check up the online help/book for info.
    STL in general can help you a lot.

    /ajk
    --
    "Those are my principles. If you don't like them I have others."
    Groucho Marx.
     
    ajk, Jan 29, 2005
    #4
  5. DamonChong

    DamonChong Guest

    Thanks alot. Kind of silly I suppose to make such mistake. ;P
     
    DamonChong, Jan 29, 2005
    #5
  6. DamonChong

    Jon Guest

    While I would agree that pointers are maybe not obvious for a beginner to
    say that there is "little use" of pointers in modern c++ is just plain wrong
    in my opinion. Of course it maybe depends on what you define as "modern c++"
    but that is another question...

    "Peter Koch Larsen" <> wrote in message
    news:%jKKd.85876$...
    > Hi Damon
    >
    > Efrat already answered your post, but I will recommend you do not use
    > new/delete as a newbie C++ programmer. Instead focus on the standard
    > library and just don't use pointers.
    > Pointers are difficult and better not used until you understand the basics
    > of C++ - including the standard library. By going this route, you will
    > also learn how little use there actually is of pointers in modern C++.
    >
    > /Peter
    > "DamonChong" <> skrev i en meddelelse
    > news:...
    >> Hi,
    >>
    >> I am new to c++. I recently spend an enormous among of time
    >> troubleshooting a seeminly innocuous piece of code. Although I narrow
    >> down this piece of code as the culprit but I don't understand why. Can
    >> some guru help to enlighten me? Thank you.
    >>
    >> // I created an array of pointers to object pointers:
    >>
    >> Object ** obs = new Object * [9];
    >>
    >> // After that I populate this array:
    >>
    >> for(int i=0; i<9; i++){
    >> obs = new Object(i);
    >> }
    >>
    >> // After using them i deleted the array:
    >>
    >> for(int i=0; i<9; i++){
    >> delete obs;
    >> }
    >>
    >> delete [] obs;
    >>
    >> delete obs; // this is the problem, during execution the program
    >> hanged.
    >>
    >> Apparently, the above caused undefined behaviour. But I don't know why
    >> i shouldn't do that. Thanks again for any sharing!
    >>
    >> Cheers,
    >> Damon
    >>

    >
    >
     
    Jon, Jan 29, 2005
    #6
  7. DamonChong

    Efrat Regev Guest

    "DamonChong" <> wrote in message
    news:...
    > Thanks alot. Kind of silly I suppose to make such mistake. ;P
    >


    No problem. BTW, explicit memory management is both very powerful, and
    very prone to making "silly" mistakes (which I don't think are silly at
    all). Consequently, perhaps you might want to read Peter Koch Larsen's
    response (with which I completely agree).

    Specifically, you could write your code this way:

    #include <vector>

    std::vector<Object> obs[9];

    for(int i = 0; i < 9; ++i)
    obs = Object(i);

    Or if Object doesn't have a default constructor,

    #include <vector>
    #include <boost/shared_ptr.hpp>

    std::vector<boost::shared_ptr<Object> > obs(9);

    for(int i = 0; i < 9; ++i)
    obs.reset(new Object(i));

    (You could google for boost smart_ptr for the above).
     
    Efrat Regev, Jan 29, 2005
    #7
  8. DamonChong

    Bradley Guest

    My question is why dynamically allocate an array of Object pointers. Why not
    just just

    Object *obparray[9];

    ??

    Is memory that sparse now days?

    "DamonChong" <> wrote in message
    news:...
    > Hi,
    >
    > I am new to c++. I recently spend an enormous among of time
    > troubleshooting a seeminly innocuous piece of code. Although I narrow
    > down this piece of code as the culprit but I don't understand why. Can
    > some guru help to enlighten me? Thank you.
    >
    > // I created an array of pointers to object pointers:
    >
    > Object ** obs = new Object * [9];
    >
    > // After that I populate this array:
    >
    > for(int i=0; i<9; i++){
    > obs = new Object(i);
    > }
    >
    > // After using them i deleted the array:
    >
    > for(int i=0; i<9; i++){
    > delete obs;
    > }
    >
    > delete [] obs;
    >
    > delete obs; // this is the problem, during execution the program
    > hanged.
    >
    > Apparently, the above caused undefined behaviour. But I don't know why
    > i shouldn't do that. Thanks again for any sharing!
    >
    > Cheers,
    > Damon
    >
     
    Bradley, Jan 30, 2005
    #8
  9. DamonChong

    Efrat Regev Guest

    "Bradley" <> wrote in message
    news:flWKd.230262$-kc.rr.com...
    >
    > My question is why dynamically allocate an array of Object pointers. Why

    not
    > just just
    >
    > Object *obparray[9];
    >
    > ??
    >
    > Is memory that sparse now days?
    >


    It's possible that Object doesn't have a default constructor.
     
    Efrat Regev, Jan 30, 2005
    #9
  10. DamonChong

    Efrat Regev Guest

    "Efrat Regev" <> wrote in message
    news:...
    > "Bradley" <> wrote in message
    > news:flWKd.230262$-kc.rr.com...
    > >
    > > My question is why dynamically allocate an array of Object pointers. Why

    > not
    > > just just
    > >
    > > Object *obparray[9];
    > >
    > > ??
    > >
    > > Is memory that sparse now days?
    > >

    >
    > It's possible that Object doesn't have a default constructor.


    Oops! my bad - I misunderstood your post.
     
    Efrat Regev, Jan 30, 2005
    #10
  11. DamonChong

    Old Wolf Guest

    Efrat Regev wrote:
    > Specifically, you could write your code this way:
    >
    > #include <vector>
    >
    > std::vector<Object> obs[9];
    >
    > for(int i = 0; i < 9; ++i)
    > obs = Object(i);
    >
    > Or if Object doesn't have a default constructor,
    >
    > #include <vector>
    > #include <boost/shared_ptr.hpp>
    >
    > std::vector<boost::shared_ptr<Object> > obs(9);
    >
    > for(int i = 0; i < 9; ++i)
    > obs.reset(new Object(i));
    >
    > (You could google for boost smart_ptr for the above).


    You don't need to go to those lengths; this would do,
    if Object is copyable:

    .. std::vector<Object> obs;
    .. for(int i = 0; i != 9; ++i)
    .. obs.push_back( Object(i) );
     
    Old Wolf, Jan 30, 2005
    #11
  12. DamonChong

    msalters Guest

    Efrat Regev wrote:
    > Specifically, you could write your code this way:
    >
    > #include <vector>
    >
    > std::vector<Object> obs[9];
    >
    > for(int i = 0; i < 9; ++i)
    > obs = Object(i);


    Actually, you mean
    std::vector<Object> obs(9);

    which is one vector with initially 9 elements, and not
    9 vectors with initially no elements.

    With your [9] declaration, the compiler will complain that it
    can't assign Object(0) to an empty vector of Objects.

    You could also be a bit clearer, and write the size only once:

    #include <vector>

    std::vector<Object> obs; // empty

    for(int i = 0; i < 9; ++i)
    obs.pushback( Object(i) ); // add one element at a time.

    Now, there's only one occurence of 9 - if the vector should ever
    change size, you can do so in one place.

    HTH,
    Michiel Salters
     
    msalters, Jan 31, 2005
    #12
    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. walterbyrd
    Replies:
    1
    Views:
    377
    Damjan
    Apr 10, 2006
  2. mike3

    Just plain crap?

    mike3, Jun 22, 2007, in forum: C++
    Replies:
    6
    Views:
    336
  3. Ark Khasin

    Is it just me or just Microsoft?

    Ark Khasin, Jul 15, 2007, in forum: C++
    Replies:
    24
    Views:
    763
    Lionel B
    Jul 16, 2007
  4. Andy B.
    Replies:
    3
    Views:
    1,402
    sloan
    Feb 24, 2009
  5. dan baker

    using 'DB_File' versus just plain tie() ?

    dan baker, Jul 2, 2003, in forum: Perl Misc
    Replies:
    5
    Views:
    120
    Michele Dondi
    Jul 5, 2003
Loading...

Share This Page