Question:
C++ assert, can it be trusted?
?
2010-01-26 10:28:22 UTC
I'm trying to test my class and it's function implementations but when I try to use the function and then cout the result it doesn't show it on screen and then when I debug it says exited with code 0 (0*0)
but then when I use assert it returns true.
so should I just trust assert or see if there's something wrong?
the problem could be with my anti-virus because every time I run the program it detects it as "security risk". (annoying!! but i turn it off when I want to run it)
Three answers:
Mantis
2010-01-26 11:32:13 UTC
Some virus programs get really nervous when programs try to write .EXE files, which is what your compiler is doing. There may be a setting in the antivirus program you can tweak. If this isn't it (or the virus checker doesn't object to other C++ programs you are compiling), try commenting out portions of your code until you find out what it is about your program the virus checker doesn't like.



Asserts are 100% safe and 100% trustable... if your code is correct. A C++ assert will not expose you to any virus, nor should it be affected in any way by a virus checker. An assert is little more than an "if" statement that dissappears in release mode.



So here's what you need to know about asserts:



1. They typically aren't there in release mode, so if you're compiling for release the assert may appear to not trigger, but could actually be ignored. (This will depend on the type of asssert you are using).



2. You should never do anything inside an assert that can affect things. Like "ASSERT(x = x + 1)." This causes big and hard-to-find bugs because the behavior changes in a release build.



3. Asserts do not and should not replace normal debugging. They are there to watch for unexpected and undesirable data. It's also not supposed to be your primary error checking mechanism... it's for program errors, not routine errors (like read/write access problems).



I love asserts and use them all over. I believe in them and encourage others to write them. But the better way to do what you're trying to do is to use a debugger. Step through the code with your debugger and you should be able to find out what's going on faster and more easily than with an assert alone. Make friends with your debugger. Any time you invest in learning to step through your program will will be repaid 100 times over. Trust me.



Good luck.
GZ
2010-01-26 10:31:56 UTC
Using the system() command from within your C++ program can cause anti-viruses to alert you. I suggest you avoid that function, there are a few alternatives.
?
2010-01-26 10:30:49 UTC
nope. it can't.







bye.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...