473,326 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Some basic questions

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
6 1723
"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
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
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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: AK | last post by:
I don't want any part of the previous discussion on Visual Basic versus Visual Basic.Net. My query is about using Visual Basic for Applications; and whether it is better to use Visual Basic 6 or...
2
by: Steven O. | last post by:
First, this may not be the correct newsgroup. I have some relatively basic questions on SQL. I tried to find a newsgroup that was specifically just about SQL, and was surprised to find that all...
7
by: Steven O. | last post by:
I am basically a hobbyist programmer, at the moment doing a little work experimenting with some AI stuff. I learned C++, and then tried to teach myself MFC using MS Visual C++ 6.0. I swore off of...
12
by: Vibhajha | last post by:
Hi friends, My sister is in great problem , she has this exam of C++ and she is stuck up with some questions, if friends like this group provides some help to her, she will be very grateful....
193
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I...
1
by: danilo of TUP | last post by:
Greetings, im Danilo, a student from TUP Manila and taking up COMPUTER SCIENCE. we are going to have a defense or we could say a project in turbo c. i just wanna know how to use the SWITCH...
3
by: aziz001DETESTSPAM | last post by:
I've been following some tutorials and reading books and have a basic grasp of the fundamental ADO.NET commands. But all the books I've read use a database with only 1 table. My database has many,...
2
by: Water Cooler v2 | last post by:
I've not touched SQL server programming since 1999. I have very little memory of it and need some clarifications on some basic questions that I could even use a book for. Until I get myself a good...
0
by: software2006 | last post by:
ASP And Visual Basic Interview questions and answers I have listed over 100 ASP and Visual Basic interview questions and answers in my website...
3
by: alcapoontje | last post by:
hi there, i'm already searching the internet for a couple of days to find an answer on this (simple) questions but i couln't find anything usefull. - how to quit and open a programm like "word" with...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.