asset ( condition ) vs try catch Block .......... which is better

P

Pallav singh

Hi

i seen code written using asset ( condition ) and try catch Block as
to Handle exception Condition ?
which one is better approach ?

when assert call terminate( ) .... does it clear all dynamic memory
allocated ?

Thanks
Pallav Singh
 
I

Ian Collins

Pallav said:
Hi

i seen code written using asset ( condition ) and try catch Block as
to Handle exception Condition ?
which one is better approach ?

Assertions and exceptions serve different purposes. The former enforces
a contract, the latter indicated a (manageable) run time error.
when assert call terminate( ) .... does it clear all dynamic memory
allocated ?

If an application ends, how can it hold memory?
 
G

Goran

Hi

i seen code written using asset ( condition ) and try catch Block as
to Handle exception Condition ?
which one is better approach ?

Bad question. These are two very, very different things. First off,
assert is a macro that does nothing if not a debug build. Exception is
always thrown.

Also, assert will call abort, whereas exception from within a try/
catch will not (or at least, there has to be some other bug for that
to happen).

WRT memory: none have absolutely __nothing__ to do with memory
allocated by the rest of the code. That is to say, memory is not
deallocated just because assert fired, or because an exception is
thrown/caught.

However, depending on your runtime environment, memory might be given
back upon program termination. In particular, if we are talking about
modern desktop/server environments (operating systems), then yes,
memory will be given back to the system, but that has __absolutely__
nothing to do with assert, try/catch, or abort. That is because
operating system does that all by itself.

Goran.
 
J

James Kanze

Bad question. These are two very, very different things. First
off, assert is a macro that does nothing if not a debug build.

I'm not sure what you mean by "not a debug build". The assert
macro does nothing if NDEBUG is not defined when the header is
included; unless you define NDEBUG (and you almost never will),
the assert macro terminates your program if the condition isn't
true.

(About the only time you'd define NDEBUG is if the profiler says
you have to. And then, you'd normally only do so around the
critical function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top