various way to delete pointer to pointer

M

morz

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
 
J

John Carson

morz said:
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.
 
K

Kai-Uwe Bux

morz said:
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
 
B

Ben Bacarisse

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.
 
D

Daniel T.

"morz said:
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
 
M

Marco Wahl

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;
}

]
 
M

morz

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top