Visual C++ “Debug Assertion Failed”

和自甴很熟 提交于 2019-12-28 06:50:53

问题


I had compiled a code using Visual Studio 2010, then I tried to run it, unfortunately during testing sometimes I had errors "Debug Assertion Failed!"....Expression (unsingned)(c+1)<=256

Then I tried the same scenario but using gcc and it ran without any problem. any idea how about this problem?


回答1:


You're calling a function like isalpha() or isdigit() with an integer whose value is not a single byte 0-255.




回答2:


There is a piece of code that says "at this point, we expect the expression (unsigned)(c + 1) <= 256 to be true; if it isn't, please stop execution at this point and break into the debugger".

The method to break into the debugger is platform dependent and probably not implemented correctly for gcc. I'd look for this piece of code in the project and then try to find out why c is supposed to be less or equal to 255, and what makes it go out of range; letting the program run to the point where the assertion is triggered gives you an implicit breakpoint on the error condition, start with that.




回答3:


I found that the problem cause is that how each compiler declare character. In Visual Studio, the default is Signed char. So each character is signed unless you explicitly precede its declaration with unsigned word. So in VS the range of the character is -128 to 127, and if the read char has an ASCII greater than 128, it will have a negative code in VS. And since this case is not handled in the functions isalpha, isdigit, etc. the function will failed. In gcc, the used methodology to set char to sign or unsigned is something like dynamic pre-processor.




回答4:


You have fallen victim of undefined behaviour with regards to unexpected input of the <ctype.h> functions. According to section 7.4, paragraph 1 of the C11 standard:

In all cases the argument is an int, the value of which shall be representable as an unsigned char or shall equal the value of the macro EOF. If the argument has any other value, the behavior is undefined.

In other words, if x isn't in the range of [0..UCHAR_MAX] or EOF, you must not pass it to any of the <ctype.h> functions. The assertion being thrown is a rather courteous way of letting you know you've violated this rule; most would just let horrible things happen (e.g. I can imagine heartbleedish bugs).



来源:https://stackoverflow.com/questions/8877410/visual-c-debug-assertion-failed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!