Click here to Skip to main content
15,912,977 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Generalrealted to new operator. Pin
anju3-Apr-03 16:39
anju3-Apr-03 16:39 
GeneralRe: realted to new operator. Pin
Dave Bryant3-Apr-03 16:52
Dave Bryant3-Apr-03 16:52 
GeneralRe: realted to new operator. Pin
Chintan3-Apr-03 18:01
Chintan3-Apr-03 18:01 
GeneralRe: realted to new operator. Pin
Dave Bryant3-Apr-03 18:06
Dave Bryant3-Apr-03 18:06 
GeneralRe: realted to new operator. Pin
Chintan3-Apr-03 18:34
Chintan3-Apr-03 18:34 
GeneralRe: realted to new operator. Pin
Dave Bryant3-Apr-03 18:37
Dave Bryant3-Apr-03 18:37 
GeneralRe: realted to new operator. Pin
Chintan3-Apr-03 19:01
Chintan3-Apr-03 19:01 
GeneralRe: realted to new operator. Pin
nde_plume4-Apr-03 5:08
nde_plume4-Apr-03 5:08 
I thought it might be useful to explain a little
more of the reason why auto_ptr was included in
the standard. (There was a great deal of haggling
about this and other types of pointer wrappers,
and there are a number of other pointer wrappers
in boost (boost.org) that will almost certainly
make it into the next version of the standard.)

The reason why auto_ptr is important relates to
exceptions. Consider this:

class excep { /* some exception class */};

void g()
{
try { f(); }
catch(excep &e)
{ handle(e); }
}

void f()
{
int* a = new int;
*a = might_throw_excep();
other_fn();
delete a;
};

Now consider what happens if might_throw_excep throws
an exception. In that case, the stack unwinds to
the nearest matching catch statement (in g), but that
means that a, which is now out of scope, points to
heap allocated memory that will never be deleted.

auto_ptr solves this problem, by rewriting f() as the
following:

void f()
{
auto_ptr<int> a = new int;
*a = might_throw_excep();
other_fn();
}

In this case, the destructor of a (which is an auto_ptr
template class) is called when it goes out of scope. This
causes the object a to be deleted. However, what is more
important is this: even if might_throw_excep throws, then
as part of the exception processing system, the destructors
of the classes are called as the stack is unwound to the
catch statement. This happens even though other_fn is not
called. That means that, even if an exception is
thrown, a's destructor is called, and it doesn't leak
memory.

For that reason, auto_ptr (or something like it) is
necessary to write leak free code in the presence of
exceptions. This is the motivation, AFAIK, behind why
auto_ptr was added to the standard.

HTH.


GeneralProblem with wincrypt.h Pin
Nick Parker3-Apr-03 16:19
protectorNick Parker3-Apr-03 16:19 
GeneralRe: Problem with wincrypt.h Pin
Zdeslav Vojkovic4-Apr-03 0:33
Zdeslav Vojkovic4-Apr-03 0:33 
GeneralRe: Problem with wincrypt.h Pin
jhwurmbach4-Apr-03 1:30
jhwurmbach4-Apr-03 1:30 
GeneralRe: Problem with wincrypt.h Pin
Zdeslav Vojkovic6-Apr-03 21:07
Zdeslav Vojkovic6-Apr-03 21:07 
GeneralRe: Problem with wincrypt.h Pin
Nick Parker4-Apr-03 3:27
protectorNick Parker4-Apr-03 3:27 
Generalerror RC2135: file not found: MENU Pin
ElizabethC3-Apr-03 13:07
ElizabethC3-Apr-03 13:07 
QuestionCan not find the Dialog resource from .NET Pin
ElizabethC3-Apr-03 12:52
ElizabethC3-Apr-03 12:52 
GeneralCString and WriteFile Pin
andyg.1013-Apr-03 11:39
andyg.1013-Apr-03 11:39 
GeneralRe: CString and WriteFile Pin
John R. Shaw3-Apr-03 12:43
John R. Shaw3-Apr-03 12:43 
GeneralRe: CString and WriteFile Pin
Chintan3-Apr-03 18:46
Chintan3-Apr-03 18:46 
GeneralRe: CString and WriteFile Pin
Joan M3-Apr-03 22:14
professionalJoan M3-Apr-03 22:14 
GeneralRe: CString and WriteFile Pin
andyg.1015-Apr-03 22:15
andyg.1015-Apr-03 22:15 
Questionhow to get main explorer's url Pin
mweiss3-Apr-03 11:02
mweiss3-Apr-03 11:02 
AnswerRe: how to get main explorer's url Pin
mweiss3-Apr-03 11:39
mweiss3-Apr-03 11:39 
GeneralCatching Keys Pressed from anywhere... Pin
Nitron3-Apr-03 10:31
Nitron3-Apr-03 10:31 
GeneralRe: Catching Keys Pressed from anywhere... Pin
Alvaro Mendez3-Apr-03 10:56
Alvaro Mendez3-Apr-03 10:56 
GeneralRe: Catching Keys Pressed from anywhere... Pin
Nitron3-Apr-03 10:58
Nitron3-Apr-03 10:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.