By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,225 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

Some basic questions

P: n/a
ccs
Why should I use auto_ptr if all the "new" and "delete" handles things
properly

What is it for auto_ptr to have release()? When should call it?

Why some people suggest using std::vector to handle 2-dimension arrays? Does
it have too much overhead compared to using "new" and "delete" directly?

Thanks in advance!

Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"ccs" <cc*@stopspamming.com> wrote in message
news:CC*****************@bgtnsc05-news.ops.worldnet.att.net...
Why should I use auto_ptr if all the "new" and "delete" handles things
properly
If 'new' and 'delete' do everything you need, then
they're all you need.
What is it for auto_ptr to have release()? When should call it?
'auto_ptr' is a pointer type with 'ownership semantics'.
Read about it in a good C++ text. An 'auto_ptr' cannot
be 'called'; it's not a function, it's an object. An
object cannot be 'called', only a function can.
Why some people suggest using std::vector to handle 2-dimension arrays?
Because they obviate the need to manage memory manually.
Does
it have too much overhead compared to using "new" and "delete" directly?


"Too much" is a subjective concept. It means nothing without
context.

-Mike
Jul 22 '05 #2

P: n/a
ccs wrote:
Why should I use auto_ptr if all the "new" and "delete" handles things
properly
That depends on where you want to use it.
What is it for auto_ptr to have release()? When should call it?
Whenever you want to release the object from the auto_ptr. auto_ptr will
destroy the object it points to as soon as it's destroyed itself, and
if you don't want that, you release it.
Why some people suggest using std::vector to handle 2-dimension
arrays?
Because in many (most) circumstances, vectors are to be preferred over
arrays because they handle their memory on their own and they are
copyable, and for some other reasons.
Does it have too much overhead compared to using "new" and
"delete" directly?


Define "too much".

Jul 22 '05 #3

P: n/a
Mike Wahler wrote:
What is it for auto_ptr to have release()? When should call it?


'auto_ptr' is a pointer type with 'ownership semantics'.
Read about it in a good C++ text. An 'auto_ptr' cannot
be 'called'; it's not a function, it's an object. An
object cannot be 'called', only a function can.


I guess you overread the part about release()?

Jul 22 '05 #4

P: n/a
ccs wrote:
Why should I use auto_ptr if all the "new" and "delete" handles things
properly

In my opinion, auto_ptr has some nice characteristics to protect your
code against exceptional conditions.

Eg:
{
std::auto_ptr<T> aptr(new T());
...
...
throw "Improperly thrown exception";
...
}

Since there is tack unwinding, auto_ptr will cleanup the memory
allocated by T, whereas

{
T* pT = new T();
...
...
throw "Another improperly thrown exception";
...
...
delete T;
}

will leak memory.
What is it for auto_ptr to have release()? When should call it?

The only place I can think of where release is harmless is when you want
to force the deletion of the pointer.

This is rarely useful, and it can be accmolpished by just doing
mPtr = std::auto_ptr<T>(0);
Why some people suggest using std::vector to handle 2-dimension arrays? Does
it have too much overhead compared to using "new" and "delete" directly?

Managed memory, support for many algorithms...
For numerical types, I prefer valarray...

JLR
Jul 22 '05 #5

P: n/a
In article <CC*****************@bgtnsc05-news.ops.worldnet.att.net>,
"ccs" <cc*@stopspamming.com> wrote:
Why should I use auto_ptr if all the "new" and "delete" handles things
properly
You shouldn't. However, if new and delete don't handle things properly
then you should. For example:

void fnc() {
int* a = new int;
int* b = new int;
// do something
delete b;
delete a;
}

Here new and delete don't handle things properly. If the second new
throws, then the first block of memory won't be deleted. If any code
inside "do something" throws, neither block will be deleted. Instead:

void fnc() {
auto_ptr<int> a( new int );
auto_ptr<int> b( new int );
// do something
}

Now the blocks of memory will get deleted properly.

What is it for auto_ptr to have release()? When should call it?
Given this function:

Viehicle* func() {
// do some side effect
return new Car;
}

If someone is calling func just for the side effect, then they will:

func();

and memory will leak. However:

auto_ptr<Vechicle> func() {
// do some side effect
return auto_ptr<Vechile>( new Car );
}

Now the call:

func();

Won't leak memory because the auto_ptr will clean up after itself. But
I'll need release if I want to get that vehicle...

Vehicle* v;
v = func().release();

Why some people suggest using std::vector to handle 2-dimension arrays?
I'm not one of those people, I think you should use a custom 2D array
class that uses std::vector internally.

Does it have too much overhead compared to using "new" and "delete" directly?


No. There is aboslutly no reason why using vector should have any more
overhead than creating and maintaining dynamic arrays on your own would.
Jul 22 '05 #6

P: n/a

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:ca*************@news.t-online.com...
Mike Wahler wrote:
What is it for auto_ptr to have release()? When should call it?


'auto_ptr' is a pointer type with 'ownership semantics'.
Read about it in a good C++ text. An 'auto_ptr' cannot
be 'called'; it's not a function, it's an object. An
object cannot be 'called', only a function can.


I guess you overread the part about release()?


Yes. Sorry about that. You've already explained it,
so now I need not.

-Mike
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.