SHORT Re: Suggestion: "fix" assert(obj)

Leandro Lucarella llucax at gmail.com
Thu Jun 21 10:19:51 PDT 2007


Walter Bright, el 15 de junio a las 21:33 me escribiste:
> Georg Wrede wrote:
> >Walter Bright wrote:
> >>Kristian Kilpi wrote:
> >>
> >>>The problem is that
> >>>
> >>>  assert(obj);
> >>>
> >>>does not first check if 'obj' is null.
> >>
> >>
> >>Yes it does, it's just that the hardware does the check, and gives you a  seg fault exception if it is null.
> >Asserts were INVENTED to *avoid segfaults*.
> 
> I don't know when assert first appeared. But I first encountered them in the 80's, when the most popular machine for programming was the x86. The x86 had no hardware 
> protection. When you wrote through a NULL pointer, you scrambled the operating systems, and all kinds of terrible, unpredictable things ensued. Asserts were used a lot to 
> try and head off these problems.
> 
> Enter the 286. What a godsend it was to develop in protected mode, when if you accessed a NULL pointer you got a seg fault instead of a scrambled system. Nirvana! What was 
> even better, was the debugger would pop you right to where the problem was. It's not only asserts done in hardware, it's asserts with:
> 
> 1) zero code size cost
> 2) zero runtime cost
> 3) they're there for every pointer dereference
> 4) they work with the debugger to let you know exactly where the problem is
> 
> Seg faults are not an evil thing, they're there to help you. In fact, I'll often *deliberately* code them in so the debugger will pop up when it hits them.

OTOH, wasn't assert included as a language construct so it can throw an
exception, giving the user the option to continue with the execution and
try to fix the situation?

I find this very useful por applications where high-availability is
crucial, I can allways catch any exception (even an assertion) and try to
continue working.

Sure you can handle a segfault with a signal handler, but the handling
code must be separated from the point of failure, making almost impossible
to take a meaningful action.

-- 
LUCA - Leandro Lucarella - Usando Debian GNU/Linux Sid - GNU Generation
------------------------------------------------------------------------
E-Mail / JID:     luca at lugmen.org.ar
GPG Fingerprint:  D9E1 4545 0F4B 7928 E82C  375D 4B02 0FE0 B08B 4FB2 
GPG Key:          gpg --keyserver pks.lugmen.org.ar --recv-keys B08B4FB2
------------------------------------------------------------------------
Peperino nos enseña que debemos ofrendirnos con ofrendas de vino si
queremos obtener la recompensa de la parte del medio del vacío.
	-- Peperino Pómoro



More information about the Digitalmars-d mailing list