By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,301 Members | 1,340 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,301 IT Pros & Developers. It's quick & easy.

Proper Way to define an ASSERT macro

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Efrat Regev wrote:
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>.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #2

P: n/a
gb
Efrat Regev wrote:
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

Jul 23 '05 #3

P: n/a
gb wrote:
Efrat Regev wrote:
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.


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();

Jul 23 '05 #4

P: n/a
gb
Rolf Magnus wrote:
gb wrote:

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.
What's another 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();


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

Jul 23 '05 #5

P: n/a
gb
gb wrote:
Rolf Magnus wrote:
gb wrote:

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.


What's another reason?


Obviously, to create a compound statement in a macro.

Gregg

Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.