|
-
May 30th, 2009, 12:32 PM
#16
Re: Using goto in c++ code
What occurs to me is that it seems obvious, without any further explication on the matter, that releasing memory does not happen unless an allocation is performed.
This is C++, right?
Here's another approach plan
Code:
int SomeFunction(int parameter)
{
if ( Initiate() )
{
foo1();
shared_ptr<t> p( AllocateMemory() );
if ( p )
{
foo2();
if ( RunAnalysis() )
{
foo3();
return true;
}
}
}
return false;
}
Or if your coding standard is strict regarding only one return place
Code:
int SomeFunction(int parameter)
{
bool rval = false;
if ( Initiate() )
{
foo1();
shared_ptr<t> p( AllocateMemory() );
if ( p )
{
foo2();
if ( RunAnalysis() )
{
foo3();
rval = true;
}
}
}
return rval;
}
And it seems reasonable that 3 indentations is at or approaching that "enough" limit, plus the rest may likely be better inside type t:
Code:
int SomeFunction(int parameter)
{
if ( Initiate() )
{
foo1();
shared_ptr<t> p( AllocateMemory() );
if ( p )
{
return p->Process( parameter );
}
}
return false;
}
bool t::Processing( int parameter )
{
bool rval = false;
foo2();
if ( RunAnalysis() )
{
foo3();
rval = true;
}
return rval;
}
In this latter case, foo2 and foo3 may well be part of class t, as they are only performed if things are going well, appear 'part of' what's happening with regard to t, at least after initializations occurred.
Now riddle me this:
What happens to allocated memory in the context of exceptions thrown by foo3, foo2 or RunAnalysis in the "goto" versions above?
And, what are your options about that?
..think about it...
Last edited by JVene; May 30th, 2009 at 03:12 PM.
If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|