Some basic questions

C

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!
 
M

Mike Wahler

ccs said:
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
 
R

Rolf Magnus

ccs said:
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".
 
R

Rolf Magnus

Mike said:
'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()?
 
J

Jorge Rivera

ccs said:
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 said:
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
 
D

Daniel T.

ccs said:
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.
 

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,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top