简体   繁体   English

VS2013的奇怪行为

[英]Strange behavior of VS2013

Recently I was bothering by a crash of my program in release mode while runs fine under debug mode. 最近,我在发布模式下崩溃,而在调试模式下却无法正常运行。

By inspecting deeply into my code I found that I forget to return true at the end of a function, which causes the crash. 通过深入检查我的代码,我发现我忘记在函数末尾return true ,这会导致崩溃。 The function should return false when fail, otherwise, it returns true. 如果失败,该函数应返回false,否则返回true。

I am wandering whether this is a defect of the compiler(vs 2013) as it (maybe) added for me the return true statement at the end of the function, however it did not when releasing. 我在徘徊这是否是编译器的缺陷(与2013年相比),因为它(也许)为我在函数末尾添加了return true语句,但是在发布时却没有。 Consequently, the programmer will spent lots of time in debugging the fault, although, the programmer should blame. 因此,程序员应该花很多时间来调试故障,尽管程序员应该对此负责。

:) :)

Flowing off the end of a function that is supposed to return a value is undefined behavior. 从应该返回值的函数的末尾流出是未定义的行为。 Undefined behavior means the compiler can do anything and still be compliant. 未定义的行为意味着编译器可以执行任何操作并且仍然合规。 Giving a warning message is compliant. 发出警告消息是合规的。 Not giving a warning message is compliant. 不发出警告消息是合规的。 Erasing your hard drive: That's also compliant. 擦除硬盘驱动器:这也符合要求。 Fortunately for me, that hasn't happened yet. 对我来说幸运的是,这还没有发生。 I've had the misfortune of invoking undefined behavior many, many times. 我经历了很多次调用未定义行为的不幸。

One reason this is undefined behavior is because there are some weird cases where flow analysis can't decide whether a function returns a value. 这是不确定行为的一个原因是,在某些奇怪的情况下,流分析无法确定函数是否返回值。 Another reason is that you might have used assembly to set the return value in a way that works just fine on your computer. 另一个原因是,您可能已经使用汇编程序以一种可以在计算机上正常工作的方式来设置返回值。 A third reason is that the compiler has to do flow analysis to make this determination; 第三个原因是编译器必须进行流量分析才能确定。 this is something many compilers don't do unless optimization is enabled. 除非启用优化,否则许多编译器不会这样做。

That said, a lack of a return before the close brace will often trigger a compiler to check whether the function returns a value. 也就是说,在右括号之前缺少返回值通常会触发编译器检查函数是否返回值。 The compiler was being nice to you when it issued a warning. 编译器发出警告时对您很好。

That you received a warning message and ignored it -- Never do that. 您收到警告消息并忽略了它-绝对不要这样做。 Compile with flags set to a reasonably high level and address each and every warning. 使用设置为较高级别的标志进行编译,并处理每个警告。 Code should always compile clean. 代码应始终编译干净。 Always. 总是。

C and C++ are tolerant languages. C和C ++是容忍的语言。 When the programmer writes code that the compiler can compile even if it looks weird , the compiler emits a warning. 当程序员编写即使看起来很奇怪也可以编译的代码时,编译器会发出警告。 The warning means you are writing something that may contain an error, but you take the decision . 警告表示您正在写可能包含错误的内容,但是要做出决定

It allows to voluntarily do certain optimisations. 它允许自愿进行某些优化。 For example, you can always use an 2D array as an 1D array what could not be done in some other languages. 例如,您始终可以将2D数组用作1D数组,而在某些其他语言中则无法做到。 But the counterpart is never ignore a warning if you are not sure you know why you are forcing the compiler to to something it does not like 但是, 如果您不确定自己为什么要强迫编译器执行其不喜欢的操作,则对方永远不会忽略警告。

Conclusion : as soon as he has ignored a warning that at the end leads to the error, the programmer is to blame ;-) 结论:只要他忽略了最终导致错误的警告, 程序员就应该责备;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM