Proper Way to define an ASSERT macro

P

Pete Becker

Efrat said:
I'd like to define an ASSERT macro that runs in debug/release modes
orthogonal to those of the compiler (I'm debuggin thread-races), and prints
file + line info. To my embarassment, I can't remember the proper way to
define the macro - I seem to vaguely recall something about defining
something like do{ ... }while(0) for some reason, but I can't remember why.
So, if someone could post a 3-5 line ASSERT macro, I'd really appreciate it.

Look in the header <assert.h>.
 
G

gb

Efrat said:
I'd like to define an ASSERT macro that runs in debug/release modes
orthogonal to those of the compiler (I'm debuggin thread-races), and prints
file + line info. To my embarassment, I can't remember the proper way to
define the macro - I seem to vaguely recall something about defining
something like do{ ... }while(0) for some reason, but I can't remember why.
So, if someone could post a 3-5 line ASSERT macro, I'd really
appreciate it.

You can look at the cassert or assert.h header files provided by your
compiler to see how the its version is definied.

When you see code like:

do {
int a;
// Other stuff
}
while (0)

it is usually just a way of creating a scope for the local variable.
You could also leave out the do and the while

{
int a;
// Other stuff
}

but some prefer the dummy do/while so it doesn't look so strange.

In the case of the assertion, it is probably not so much for lexical
scoping as it is for the runtime effect of creating a stack frame for a
breakpoint.

Gregg
 
E

Efrat Regev

Hello,

I'd like to define an ASSERT macro that runs in debug/release modes
orthogonal to those of the compiler (I'm debuggin thread-races), and prints
file + line info. To my embarassment, I can't remember the proper way to
define the macro - I seem to vaguely recall something about defining
something like do{ ... }while(0) for some reason, but I can't remember why.
So, if someone could post a 3-5 line ASSERT macro, I'd really appreciate it.

Many Thanks,

Efrat
 
R

Rolf Magnus

gb said:
appreciate it.

You can look at the cassert or assert.h header files provided by your
compiler to see how the its version is definied.

When you see code like:

do {
int a;
// Other stuff
}
while (0)

it is usually just a way of creating a scope for the local variable.

That's not the only reason.
You could also leave out the do and the while

{
int a;
// Other stuff
}

but some prefer the dummy do/while so it doesn't look so strange.

No, they prefer it so that things like the folling don't produce a compile
error:

if (foo)
MYMACRO(whatever);
else
blah();
 
G

gb

Rolf said:
variable.

That's not the only reason.

What's another reason?
No, they prefer it so that things like the folling don't produce a compile
error:

if (foo)
MYMACRO(whatever);
else
blah();

You are correct, I did not consider that this construct would typically
be found in a macro, admittedly the subject of the OP. I was thinking
of its use in a non-macro context.

Gregg
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top