Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > various way to delete pointer to pointer

Reply
Thread Tools

various way to delete pointer to pointer

 
 
morz
Guest
Posts: n/a
 
      02-05-2006
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[i];
delete ptr;

But can i delete like this? :

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

or

can somebody give any other ways or sample?

P/S: i just like master in deep about pointer

 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      02-05-2006
"morz" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com
> 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[i];
> delete ptr;
>
> But can i delete like this? :
>
> delete ptr;
> for (int i = 0; i <2; i++)
> delete ptr[i];


No, because after the

delete ptr;

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

--
John Carson



 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      02-05-2006
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[i];
> delete ptr;


Correct.


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


No, you cannot: Within the for loop you are dereferencing the previously
deleted pointer ptr. The expression ptr[i] 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
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-05-2006
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[i];
> dele use ote ptr;


delete [] ptr;

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


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.

 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      02-05-2006
In article <(E-Mail Removed) .com>,
"morz" <(E-Mail Removed)> 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[i];
> delete ptr;
>
> But can i delete like this? :
>
> delete ptr;
> for (int i = 0; i <2; i++)
> delete ptr[i];
>
> 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.
 
Reply With Quote
 
Marco Wahl
Guest
Posts: n/a
 
      02-06-2006

>> i know we can delete ptr like this:
>>
>> for (int i = 0; i <2; i++)
>> delete ptr[i];
>> 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> > >:ush_back(int*)’
/usr/include/c++/4.0.2/bits/stl_vector.h:602: note: candidates are:
void std::vector<_Tp, _Alloc>:ush_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;
}

]

 
Reply With Quote
 
morz
Guest
Posts: n/a
 
      02-06-2006
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

 
Reply With Quote
 
Marco Wahl
Guest
Posts: n/a
 
      02-06-2006
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Various template approaches to avoid pointer to function penalty StephQ C++ 5 02-28-2007 03:48 PM
way way way OT: MCNGP Announcement Neil MCSE 174 04-17-2006 05:55 PM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
AMD Opteron: 1-way, 2-way, ... Up to 8-way. John John Windows 64bit 12 12-27-2005 08:17 AM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments