473,395 Members | 1,584 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,395 software developers and data experts.

The 'finally' debate


I was just reading through some old articles in the 'Why not develop new
language' thread and came across the finally debate.

Everytime I mention 'finally' to C++ programmers I get almost emotional
responses about why it is not needed in C++. I don't get that.

For example, consider the following code. Please note, I can only use
heap allocated objects in my current project (new/delete).

//
// Foo - Tries to foo. Can throw a FooException
//

void Foo()
{
try {
// Do your foo business that could throw a FooException
}

catch (...) {
// Cleanup your business
throw;
}

// Cleanup your business
}

Now, with finally I could do this:

void Foo()
{
try {
// Do your foo business that could throw a FooException
}

finally {
// Cleanup your business
}
}

Which I find *much* cleaner than the other example as there is no
need to do the cleanup twice.

Anyway, the debate is useless because we don't have finally. So my question
really is, how do people refactor the above to something nicer?

S.
Jul 22 '05
54 2814
Stefan Arentz wrote:
Neh, we keep the beer in the fridge. The device is a MIPS based device
with not too much RAM/Flash. Think <= 8MB. which needs to be shared
with a kernel, libraries some tools.

It is not very special, you just can't use all nice tricks that are
obvious on a normal 1GB workstation with a standard 80GB drive :)


I developed most of my standard C++ library on a machine with 160MB
of disk space (yes, *disk space*, not memory). I had (newer version
of gcc unfortunately broke it) a C++ hello-world which took 4kB
(yes, 4*k*B). I think it is now ~40kB, mostly due to the exception
support I need to include. However, this was in an experimental
branch and not merged into the main library (it was mostly a demo
of what can be done; unfortunately, nobody pays me for actually
doing this to the whole standard C++ library...). With my "normal"
implementation, it indeed takes ~700kB for a hello-world (stripped
executable on an Intel machine).

Anyway, you should realize that STL != "standard C++ library": the
STL is just the generic stuff (containers, iterators, algorithms).
This will have no or at most only very small overhead compared to
manually written code.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
Jul 22 '05 #51
In article <87************@keizer.soze.com>,
Stefan Arentz <st***********@gmail.com> wrote:
al***@start.no (Alf P. Steinbach) writes:

...
Mayby someone cann tell me why this was not taken into C++


Because 'finally' is rarely needed in C++, and where it is needed it
indicates a redesign/refactoring is called for, and in the extremely
rare cases where that isn't an option, you can easily emulate it.


So how do you emulate it.

In my situation: no templates, no STL, no external libraries. Just bare
C++. GCC extensions are acceptable (3.3).

I've used inner functions (GCC extension) at one point.

void Foo()
{
vars;

void cleanup() {
cleanup vars;
}

try {
}

catch (...) {
cleanup();
throw;
}

cleanup();
}

But still, very yuckie :)

S.


struct Vars {
Vars(): vars() { }
~Vars() {cleanup vars;}
vars;
};

void Foo() {
Vars v;
// do stuff with v.vars;
}

I don't see why you are ignoring the simple and obvious solution...
Jul 22 '05 #52
In article <84*************************@posting.google.com> ,
Old Wolf <ol*****@inspire.net.nz> wrote:
Stefan Arentz <st***********@gmail.com> wrote:
"Gernot Frisch" <Me@Privacy.net> writes:
> > I'm on a device that is too small to even include STL :)
>

The device is a MIPS based device
with not too much RAM/Flash. Think <= 8MB. which needs to be shared
with a kernel, libraries some tools.


8MB ?! Here I was debating whether to use C++ (with STL) in a
device with 128K ram and 256K flash. Unfortunately the only
available C++ compilers were old, so they would not have proper
standard C++ support, and probably would be bad optimisers.
If the platform supported one of the 'mainstream' C++ compilers
(eg. Gnu, Intel, MS) it would have been a more serious option
(But I probably still would have stuck to C so that the code
could port to other devices which didn't have a good C++ compiler).


It's said we have proper standard C++ support, and are always
glad to do custom ports, so feel free to email us.
--
Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Jul 22 '05 #53
In message <UU*******************@newssvr29.news.prodigy.com> , red floyd
<no*****@here.dude> writes
Rolf Magnus wrote:
Imho, an auto_ptr for
arrays should be part of the standard library.


I guess that the Committee decided that that the auto_ptr for arrays is
std::vector.


But if you actually *need* the strict-ownership move-not-copy semantics
of auto_ptr, it's not. If you don't, then boost::scoped_array is another
possibility.
--
Richard Herring
Jul 22 '05 #54
On 21 Sep 2004 18:23:34 +0200, Stefan Arentz <st***********@gmail.com>
wrote:
"Peter Koch Larsen" <pk*****@mailme.dk> writes:

...
> I'm on a device that is too small to even include STL :)
>
> S.


I do not understand what you're saying. STL - or templates - does not
necessarily use more ressources than handwritten code. In your case it
should be safe.


Templates, yes probably. But including a 700K library in the firmware is
simply not an option.


You don't need 700k. To use the STL you don't need to link any
libraries at all (assuming your allocator implementation is in the
header). If you've got enough resources to have exceptions enabled,
you've certainly got enough for the STL (which requires exception
support ideally), and
std::auto_ptr/scoped_ptr/scoped_array/std::vector/etc.

Remember, with templates, only code that is actually called gets
compiled into your .exe.

Tom
Jul 22 '05 #55

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: melledge | last post by:
Worldwide Debate on Open Data Highlighted at XTech 2005 Presentation Topics Include Web Services, RSS, FOAF, OAI, Open Access, and More;Special Focus on OpenOffice.org's Influence on Standards and...
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
77
by: berns | last post by:
Hi All, A coworker and I have been debating the 'correct' expectation of evaluation for the phrase a = b = c. Two different versions of GCC ended up compiling this as b = c; a = b and the other...
2
by: Wells | last post by:
Debate Simmering in US Over Regulation of Internet A heated debate is shaping up in Washington about a concept some activists are calling Internet network neutrality, known more popularly as net...
11
by: John A Grandy | last post by:
I'm in a vigorous debate at my work regarding objects assuming knowledge of the type their containing object. This debate pertains specifically to ASP.NET, but I have decided to post in the C#...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
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...

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.