Re: Return value(s) from functions - any defacto standard?
- From: Jimbo_Jimbob_Jiminator <JimboJimbobJiminator@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 5 Dec 2008 10:05:01 -0800
I don't see why the heap needs to be involved, unless you're counting
thread-local storage, which is what you need to use for a "last error"
variable.
Yeah, the variable is exactly it. The amount of storage is not a problem,
just making it clear. Would this space be global so a functions could recycle
the same memory block? If not, then each function needs it's own static
(heap) storage for the error reporting. Each function may need it's own
because some have much more to report than others or you make the block as
large as the largest reporting block so they can all use it. In the embedded
world, while we write in high-level languages now and don't count each byte
like we used to, we still must be conservative with RAM and FLASH, mostly RAM.
Of course, in the PC (Windows) world, reserving space for error reporting is
not really an issue. This is an MFC forum after all.
Also, you can return bool and have an (optional) error code
parameter as well; that way, you won't have a hodgepodge of functions
returning 0 for success vs. those that return true...
The optional parameter will work in C++, but not with C though.
Jim
"Doug Harrison [MVP]" wrote:
On Fri, 5 Dec 2008 07:54:01 -0800, Jimbo_Jimbob_Jiminator.
<JimboJimbobJiminator@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
Hey Doug,
Interesting thread. It started out as a discussion on bool vs. BOOL but also
addressed the return values at some point.
From your comment it seems that in C++, where exceptions are part of the
language, that most functions should return bool and the exception handler
and\or GetLastError (or equivalent) should handle the actual error reporting.
One nice thing about exceptions is that the error reporting is separated
from the return type and function parameters or helper function like
GetLastError. That is, when you use exceptions, it's common to return void,
or to return a CString or whatever, because errors are reported by throwing
exceptions. Return codes are preferred mainly for those functions whose
errors need to be handled locally, for which failure is relatively common;
you shouldn't force people to write a local try/catch every time they use a
function whose failure is anticipated to be handled locally most of the
time.
Most of my programming is C++. I still support some legacy embedded C code.
I could start applying the same principles here with GetLastError type of
call. This C code has no underlying OS not that has any bearing on it.
Sometimes , it just seems more efficient to return the error code directly if
there are only a few possible codes especially on calls that use all
automatic variables. To get last error after the call, there will have to be
some space on the heap to support that.
I don't see why the heap needs to be involved, unless you're counting
thread-local storage, which is what you need to use for a "last error"
variable. Also, you can return bool and have an (optional) error code
parameter as well; that way, you won't have a hodgepodge of functions
returning 0 for success vs. those that return true, and you don't have to
implement the "last error" approach.
--
Doug Harrison
Visual C++ MVP
- Follow-Ups:
- Re: Return value(s) from functions - any defacto standard?
- From: Doug Harrison [MVP]
- Re: Return value(s) from functions - any defacto standard?
- References:
- Return value(s) from functions - any defacto standard?
- From: Jimbo_Jimbob_Jiminator
- Re: Return value(s) from functions - any defacto standard?
- From: Doug Harrison [MVP]
- Re: Return value(s) from functions - any defacto standard?
- From: Jimbo_Jimbob_Jiminator
- Re: Return value(s) from functions - any defacto standard?
- From: Doug Harrison [MVP]
- Return value(s) from functions - any defacto standard?
- Prev by Date: Re: Return value(s) from functions - any defacto standard?
- Next by Date: Inclusion problem between two project
- Previous by thread: Re: Return value(s) from functions - any defacto standard?
- Next by thread: Re: Return value(s) from functions - any defacto standard?
- Index(es):
Relevant Pages
|