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! 6 1744
"ccs" <cc*@stopspammi ng.com> wrote in message
news:CC******** *********@bgtns c05-news.ops.worldn et.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
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".
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()?
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
In article <CC************ *****@bgtnsc05-news.ops.worldn et.att.net>,
"ccs" <cc*@stopspammi ng.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<Vechic le> func() {
// do some side effect
return auto_ptr<Vechil e>( 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.
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 Visual Basic.Net as a springboard
for studying VBA.
I use Office 2000 and would like to use VBA as a tool to customize it.
I have zero programming experience.
I would like to read through and work on the examples of a beginners
|
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 the
SQL-related newsgroups seem to be product related. But if I missed
something, and someone can steer me to a correct newsgroup, please do
so.
My specific questions:
1. I want to put comments in an SQL script. For example, I want
|
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 MFC,
which was a nightmare, and have been playing with Borlands C++
Builder. In C++ Builder, creating forms and other GUI elements is
much simpler than MFC, very similar to Visual Basic.
In the store yesterday, I was looking at the C++ .Net...
|
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.
These are some questions:-
7. design and implement a class binsearch for a binary search tree.it
includes search,remove and add options.make suitable assumption.
8.explai how pointers to functions can be declared in c++.under what
conditions can...
|
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 speak only of features in the spirit of C; something like
object-orientation, though a nice feature, does not belong in C.
Something like being able to #define a #define would be very handy,
though, e.g:
#define DECLARE_FOO(bar) #define...
| |
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 FUNCTION cause we really need it
badly.
about the array in the interger type variables, what if i put this:
int num;
then print some statements so that the user can put some values
|
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, so:
1. I know there is 1 OleDbConnectino object variable per database
file, but is there one dataAdapter per table/query? (e.g. my tables:
tblCustomer, tblProducts, tblPrices etc etc, so should I have
dataAdapters such as these?...
|
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 book, someone
please help me with the answers:
1) What are SQL functions and how are they different from stored
procedures? Do both of the programming objects not achieve the same
thing? What was the need of having one in addition to the...
|
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
http://www.geocities.com/myinterviewquestions/ASPAndVisualBasic.htm
So please have a look and make use of it.
|
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 a trigger in visual basic?
- can you run your programms without opening excell first(because i'm working with vb in excell). My brother said something of .exe files but he didn't knew how to do that in visual basic because he works with java.
...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |