various way to delete pointer to pointer

Discussion in 'C++' started by morz, Feb 5, 2006.

  1. morz

    morz Guest

    i just search for a while in c++ groups but cannot find good answer.

    i have code like this:

    int **ptr;
    ptr = new char *[2];

    ptr[0] = new int(5);
    ptr[1] = new int(16);

    i know we can delete ptr like this:

    for (int i = 0; i <2; i++)
    delete ptr;
    delete ptr;

    But can i delete like this? :

    delete ptr;
    for (int i = 0; i <2; i++)
    delete ptr;

    or

    can somebody give any other ways or sample?

    P/S: i just like master in deep about pointer
    morz, Feb 5, 2006
    #1
    1. Advertising

  2. morz

    John Carson Guest

    "morz" <> wrote in message
    news:
    > i just search for a while in c++ groups but cannot find good answer.
    >
    > i have code like this:
    >
    > int **ptr;


    This next line should have int, not char

    > ptr = new char *[2];
    >
    > ptr[0] = new int(5);
    > ptr[1] = new int(16);
    >
    > i know we can delete ptr like this:
    >
    > for (int i = 0; i <2; i++)
    > delete ptr;
    > delete ptr;
    >
    > But can i delete like this? :
    >
    > delete ptr;
    > for (int i = 0; i <2; i++)
    > delete ptr;


    No, because after the

    delete ptr;

    ptr essentially reverts to an "uninitialized" status, so ptr involves
    undefined behaviour. As with all undefined behaviour, you might get away
    with it, but it is foolish to try.

    --
    John Carson
    John Carson, Feb 5, 2006
    #2
    1. Advertising

  3. morz

    Kai-Uwe Bux Guest

    morz wrote:

    > i just search for a while in c++ groups but cannot find good answer.
    >
    > i have code like this:
    >
    > int **ptr;
    > ptr = new char *[2];
    >
    > ptr[0] = new int(5);
    > ptr[1] = new int(16);
    >
    > i know we can delete ptr like this:
    >
    > for (int i = 0; i <2; i++)
    > delete ptr;
    > delete ptr;


    Correct.


    > But can i delete like this? :
    >
    > delete ptr;
    > for (int i = 0; i <2; i++)
    > delete ptr;


    No, you cannot: Within the for loop you are dereferencing the previously
    deleted pointer ptr. The expression ptr does that! Dereferencing a
    deleted pointer is undefined behavior. This kind of undefined behavior can
    be quite nasty a bug since very often it manifests itself in expected
    behavior most of the time and explodes into your face only occasionally.

    > or
    >
    > can somebody give any other ways or sample?
    >
    > P/S: i just like master in deep about pointer


    The deep mastery of pointers is knowing all the tools that allow you *not*
    to use pointers in the first place. Pointers involve all sorts of nasty
    pitfalls: double deletion, dereferencing after deletion, missing deletion
    (aka memory leak) to name just those inherently steming from pointer use.
    The problem here is that pointer management involves life time management
    and is intrinsically a problem that tends to spread out over your code. The
    general advice here would be to have well defined ownership (each pointer
    is owned by a single object and allocation as well as deletion is the
    responsibility of that object). The best idiom for that would be to confine
    allocation of pointers to constructors and to put the corresponding
    deallocation within the destructor.

    Then there is the whole area of traps that come from the interaction of
    dynamic memory allocation and stack unwinding triggered by exceptions. Let
    me just give you the simplest example:

    {
    SomeType* some_pointer = new SomeType( some arguments );
    // some code
    ...
    delete some_pointer;
    }

    What happens if the code between the allocation and the delete throws an
    exception and the program starts stack unwinding? The delete statement will
    never be reached. However, the destructor for some_pointer will be called
    and the variable will be gone: you can never make up for the missing
    delete. BTW: if you look close enough you will find that the second
    allocation in your program may serve as the "some code" section from this
    example. In other words, your sample snippet exhibits a possible memory
    leak if the second allocation fails.

    Avoid pointers whenever possible. There are lots of tools that allow you to
    get away without touching raw pointers most of the time: shared_ptr<T>,
    auto_ptr<T>, and of course all the standard containers like std::map and
    std::vector from the standard library.


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Feb 5, 2006
    #3
  4. On Sun, 05 Feb 2006 06:51:51 -0800, morz wrote:

    > i have code like this:
    >
    > int **ptr;
    > ptr = new char *[2];
    >
    > ptr[0] = new int(5);
    > ptr[1] = new int(16);
    >
    > i know we can delete ptr like this:
    >
    > for (int i = 0; i <2; i++)
    > delete ptr;
    > dele use ote ptr;


    delete [] ptr;

    > But can i delete like this? :
    >
    > delete ptr;
    > for (int i = 0; i <2; i++)
    > delete ptr;


    No. After "delete ptr" (or, better, "delete [] ptr") ptr is invalid and
    you can't use it.

    > can somebody give any other ways or sample?


    You can make an array of "smart pointers". Then, "delete [] ptr" will
    call the destructor of each element and the smart pointer will delete the
    thing it points to.

    --
    Ben.
    Ben Bacarisse, Feb 5, 2006
    #4
  5. morz

    Daniel T. Guest

    In article <>,
    "morz" <> wrote:

    > i just search for a while in c++ groups but cannot find good answer.
    >
    > i have code like this:
    >
    > int **ptr;
    > ptr = new char *[2];


    int or char?

    > ptr[0] = new int(5);
    > ptr[1] = new int(16);
    >
    > i know we can delete ptr like this:
    >
    > for (int i = 0; i <2; i++)
    > delete ptr;
    > delete ptr;
    >
    > But can i delete like this? :
    >
    > delete ptr;
    > for (int i = 0; i <2; i++)
    > delete ptr;
    >
    > or
    >
    > can somebody give any other ways or sample?


    vector<shared_ptr<int> > ptr;
    ptr.push_back( new int(5) );
    ptr.push_back( new int(16) );

    // no deleting necessary


    --
    Magic depends on tradition and belief. It does not welcome observation,
    nor does it profit by experiment. On the other hand, science is based
    on experience; it is open to correction by observation and experiment.
    Daniel T., Feb 5, 2006
    #5
  6. morz

    Marco Wahl Guest


    >> i know we can delete ptr like this:
    >>
    >> for (int i = 0; i <2; i++)
    >> delete ptr;
    >> delete ptr;


    >> can somebody give any other ways or sample?


    This code-fragment does not compile with my compiler.

    > vector<shared_ptr<int> > ptr;
    > ptr.push_back( new int(5) );
    > ptr.push_back( new int(16) );
    >
    > // no deleting necessary


    The error is something like

    g++ smart_ptrexapl.cpp -o smart_ptrexapl
    smart_ptrexapl.cpp: In function ‘int main()’:
    smart_ptrexapl.cpp:10: error: no matching function for call to
    ‘std::vector<std::tr1::shared_ptr<int>,
    std::allocator<std::tr1::shared_ptr<int> > >::push_back(int*)’
    /usr/include/c++/4.0.2/bits/stl_vector.h:602: note: candidates are:
    void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp =
    std::tr1::shared_ptr<int>, _Alloc =
    std::allocator<std::tr1::shared_ptr<int> >]

    The following program is a correction of Daniel T.s fragment.

    [

    #include <tr1/memory>
    #include <vector>

    using std::tr1::shared_ptr;
    using std::vector;

    int main() {
    vector<shared_ptr<int> > ptr;
    ptr.push_back(shared_ptr<int>(new int(42)));
    return 0;
    }

    ]
    Marco Wahl, Feb 6, 2006
    #6
  7. morz

    morz Guest

    int **ptr;
    ptr = new char *[2];

    acatually the char is int.sorry for the typo.

    Wow! everybody gave good answers.

    i'm interested with Daniel T. code:

    vector<shared_ptr<int> > ptr;
    ptr.push_back( new int(5) );
    ptr.push_back( new int(16) );

    // no deleting necessary

    and Marco Wahl code :

    #include <tr1/memory>
    #include <vector>

    using std::tr1::shared_ptr;
    using std::vector;

    int main() {
    vector<shared_ptr<int> > ptr;
    ptr.push_back(shared_ptr<int>(new int(42)));
    return 0;

    }

    i would like to know any other way create pointers without delete it
    after use? any other stl approach like above code? thank you
    morz, Feb 6, 2006
    #7
  8. morz

    Marco Wahl Guest

    > i would like to know any other way create pointers without delete it
    > after use? [...]


    Maybe you would like to study the auto_ptr-class that is already part
    of the c++-standard and further the remaining pointer-classes besides
    the shared_ptr-class from the boost library. You could start at
    http://www.boost.org/libs/smart_ptr/smart_ptr.htm
    Marco Wahl, Feb 6, 2006
    #8
    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. Neil Zanella

    VHDL: various questions and issues...

    Neil Zanella, Nov 17, 2003, in forum: VHDL
    Replies:
    2
    Views:
    3,854
    Charles M. Elias
    Nov 17, 2003
  2. acd
    Replies:
    0
    Views:
    440
  3. tony collier
    Replies:
    2
    Views:
    358
    tony collier
    Jan 12, 2004
  4. Sky
    Replies:
    0
    Views:
    315
  5. StephQ
    Replies:
    5
    Views:
    293
    Michael DOUBEZ
    Feb 28, 2007
Loading...

Share This Page