How to use assert?

N

none

I am trying to use assert to test an expression:

#include <assert.h>
#include <vector>


int main() {
std::vector<int> myVector;
myVector.push_back(3);
assert (myVector[0] == 444);
}


when I run this code I get no fail message. Should assert not report an error when the expression
evaluates to false?
 
J

Jonathan Lee

when I run this code I get no fail message. Should assert not report an error when the expression
evaluates to false?

Odd.. works for me:
g++ test.cpp
./a.out
a.out: test.cpp:7: int main(): Assertion `myVector[0] == 0' failed.

--Jonathan
 
N

none

Leigh said:
none said:
I am trying to use assert to test an expression:

#include <assert.h>
#include <vector>


int main() {
std::vector<int> myVector;
myVector.push_back(3);
assert (myVector[0] == 444);
}


when I run this code I get no fail message. Should assert not report
an error when the expression evaluates to false?

assert does nothing if NDEBUG is defined, try running a debug build of
your program rather than a release build.

/Leigh

Hm my debug build takes forever to compile and run so I am currently developing on a release build.
Is it not possible to undefine NDEBUG in my release code?
 
Ö

Öö Tiib

none ha scritto:




I am trying to use assert to test an expression:
#include <assert.h>
#include <vector>
int main() {
  std::vector<int> myVector;
  myVector.push_back(3);
  assert (myVector[0] == 444);
}
[...]
Hm my debug build takes forever to compile and run so I am currently
developing on a release build.

If your compiler takes forever to compile, then chances are there is
something wrong with your compiler, and you should fix that first.
Is it not possible to undefine NDEBUG in my release code?

Of course. It's even better than that: you can actually activate and
deactivate assertions separately in each compilation unit. That's so you
can leave them on by default and disable them only where appropriate.

Little side note that it is even better than that ... since <cassert>
and <assert.h> have no include guards you can include it multiple
times in single compilation unit with NDEBUG defined and undefined
depending on context as lot you please.
 
J

James Kanze

none ha scritto: [...]
Hm my debug build takes forever to compile and run so I am
currently developing on a release build.
If your compiler takes forever to compile, then chances are
there is something wrong with your compiler, and you should
fix that first.

Or the environment you're working in. With the default settings
for Visual Studios, debug mode compiles faster than release.
(But the default settings for both are pretty useless, and you
generally only want one mode anyway---or three, or whatever,
depending on the application.)
Of course. It's even better than that: you can actually
activate and deactivate assertions separately in each
compilation unit. That's so you can leave them on by default
and disable them only where appropriate.

The authors of the C standard carefully designed assert so that
you can turn it off on a function by function basis. It was
quite understood at the time that you would almost never want to
turn it off globally.
 

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,770
Messages
2,569,584
Members
45,078
Latest member
MakersCBDBlood

Latest Threads

Top