Re: Return value(s) from functions - any defacto standard?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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

.



Relevant Pages

  • Re: MySQL error reporting
    ... now that I think of it that may be a built in error reporting system. ... well I thought that was working but maybe just on the live server not my home test version. ... Error code 1062 is a duplicate entry error. ...
    (comp.lang.php)
  • Re: Computer Restarts While I Am Using It.
    ... I saw the 2nd Error Code when my computer crashed and showed the blue ... then to the advanced tab and then error reporting button. ... you should get a detailed message about the error causing the failure. ... For the past 2 months my computer has been restarting itself while I am ...
    (microsoft.public.windowsxp.help_and_support)