简体   繁体   English

奇怪的C ++错误,添加2条打印语句后程序运行正常,没有它们的段错误

[英]Weird C++ bug, program works fine when 2 print statements are added, segfaults without them

I am writing some code in C++ that has to compute lots of ray/object intersections and I'm getting a very weird bug that I don't understand. 我正在用C ++编写一些必须计算许多射线/对象交点的代码,但遇到了一个我不理解的非常奇怪的错误。

On some very large instances (lots of triangles and rays) my program segfaults. 在某些非常大的实例(许多三角形和射线)上,我的程序存在段错误。 I've been trying to figure out the source of these segfaults but I've been stumped. 我一直在试图找出这些段错误的来源,但我感到很困惑。 I've looked through my code and it doesn't seem like I should ever try to index off the end of an array or access a null pointer. 我已经检查了我的代码,似乎不应该尝试索引数组的末尾或访问空指针。 It also doesn't seem like my computer is running out of memory. 看来我的计算机似乎还没有内存不足。 When I monitor it, it looks like there's still a few hundred megabytes free. 当我监视它时,看起来还有几百兆的可用空间。

While debugging, I attempted the following: I inserted two print statements in my code in an attempt to determine the exact interesection computation that was causing the segfault. 在调试时,我尝试了以下操作:我在代码中插入了两个print语句,以试图确定导致段错误的精确交点计算。 Unfortunately for me, when I inserted the print statements and ran the program again, everything worked. 对我来说不幸的是,当我插入打印语句并再次运行该程序时,一切正常。 It didn't segfault. 这不是段错误。 So if I remove those two print statements and I run the program it segfaults, and if I keep them in it works fine (although much slower as it has to print everything). 因此,如果我删除了这两个打印语句并运行了该程序,则它会出现段错误,并且如果将它们保留在其中,则可以正常工作(尽管要慢得多,因为它必须打印所有内容)。 Everything else is kept exactly the same, except for the removal/addition of those print statements. 除删除/添加这些打印语句外,其他所有内容均保持完全相同。

What could possibly cause this to happen? 有什么可能导致这种情况发生? How would adding print statements to a c++ program possibly cause it to not segfault? 将打印语句添加到C ++程序如何导致其不出现段错误?

If it helps at all, the algorithm is only a single thread and I compiled everything using g++ in Linux. 如果有帮助的话,该算法只是一个线程,我在Linux中使用g ++编译了所有内容。

What could possibly cause this to happen? 有什么可能导致这种情况发生? How would adding print statements to a c++ program possibly cause it to not segfault? 将打印语句添加到C ++程序如何导致其不出现段错误?

Welcome to undefined behaviour. 欢迎来到不确定的行为。

You need to replace your arrays/pointers/etc with self-checking versions and prove , rather than guesstimate, that you don't have any bugs in these areas. 您需要用自检版本替换数组/指针/ etc,并证明 (而不是猜测)在这些方面没有任何错误。

The fact that inserting print statements "fixes" the seg fault is a clear indication that you are accessing memory that you shouldn't. 插入打印语句“修复”了段错误这一事实清楚地表明您正在访问不应访问的内存。

The best thing to do is to take out the print statements, and run your program through a debugger. 最好的办法是取出打印语句,然后通过调试器运行程序。 Since you are working in Linux, compile your program with a -g flag and run it through gdb. 由于您使用的是Linux,因此请使用-g标志编译程序并通过gdb运行它。 It might tell you exactly in which line it segfaults. 它可能会告诉您确切的段错误。

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

相关问题 c++ 没有编译器警告的指针段错误,在先前分配时有效 - c++ pointer segfaults without compiler warning, works when previously assigned C ++ libpthread程序segfaults(原因不明) - C++ libpthread program segfaults for unknown reason C ++调用getenv(“ LINES”)或getenv(“ COLUMNS”)在Xcode中运行良好,在终端中运行时出现段错误 - C++ calling getenv(“LINES”) or getenv(“COLUMNS”) runs fine in Xcode, segfaults when run in terminal Visual Studio c++ - 程序在没有调试器的情况下失败,与调试器一起工作正常 - Visual Studio c++ - program fails without debugger, works fine with debugger 将代码链接到exe时C ++程序崩溃,但是将代码编译到exe时工作正常,为什么呢? - C++ program crashes when code is linked to exe but works fine when code is compiled into exe, how come? 使用sublime-build时无法运行C ++程序,从终端运行时可以正常工作 - Cannot run C++ program when using sublime-build, works fine when running from terminal C ++-当我使用sprintf()函数时程序崩溃(std :: cout正常工作) - C++ - Program crashes when I use sprintf() function (std::cout works fine) 修改ui时出现Qt segfaults(C ++) - Qt segfaults when modifying ui (c++) 奇怪的C ++浮动bug - Weird C++ float bug 没有分号的程序在C中编译好而不是在C ++中为什么? - Program without semicolon compiles fine in C not in C++ why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM