valgrind 和常规 c++ 之间有什么区别?跑步
我正在尝试确定 我的代码中有错误,我在其中得到了 seg。尝试将值分配给向量中的指针时出现错误(链接中对此进行了更好的描述)。当我使用 valgrind 运行代码时,我没有收到 seg.fault。
valgrind 有什么不同之处。我认为我需要考虑 valgrind 会话和常规 C++ 会话之间的内存管理差异,但我真的不知道
I'm trying to identify a bug in I have in my code where I get seg. fault while trying to assign value to a pointer from a vector (it is describe better in the link). When I run the code using valgrind I don't get the seg.fault.
What does valgrind do differently. I think that I need to consider the memory management differences between valgrind session and regular c++ session but I don't really know
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
来自 Valgrind 常见问题解答:
所以你不能与它有任何关系。实际上你不必担心你的程序在 Valgrind 下不会崩溃。您应该从中读取错误消息并修复它们。从无效读取/无效写入错误开始。它们几乎总是指出代码中的错误。在这种特殊情况下,您还可以从简单的 bash 脚本无限循环中运行代码,直到它产生错误消息。您很可能正在使用无效的迭代器,这在 C++ 中是未定义行为。
From Valgrind FAQ:
So you can nothing to do with it. Actually you need not worry that you program not crashes under Valgrind. You should read error messages from it and fix them. Start with Invalid read/Invalid write errors. They are almost always indicate the bug in code. In this particular case you can also run your code in infinite loop from simple bash script utill it produces error message. Most likely you are working with invalid iterators and it is Undefined Behaviour in C++.
也许问题可能与时间相关,当您使用 valgrind 运行代码时,它的运行速度会稍慢一些,因为 valgrind 在运行时收集并诊断您的代码。
Maybe the issue might be timing dependent, When you run your code with valgrind it runs a little slower because valgrind collects and diagnoses your code at run time.
Valgrind 会跟踪您的程序的内存使用情况。这就是它告诉您泄漏的方式。这意味着它劫持了
malloc
等并使用自己的来实现这一点。这意味着,当您正常运行代码时,您可能会读/写一些意外释放的数据,从而导致段错误,而 valgrind 可能会保留该内存以查看它是否真正丢失等,从而意味着运气好记忆仍然有效。只是一个猜测。Valgrind keeps track of your programs memory usage. This is how it tells you about leaks. What this means is that it hijacks the
malloc
and such and uses its own so that it can achieve this. This means that probably when you run your code normally you read/write to some data you have freed accidentally causing segfault whereas it could be that valgrind is keeping this memory around to see if it is truly lost etc thereby meaning by (un)luck the memory is still valid. Just a guess.Valgrind 在虚拟 CPU 上运行您的程序,也就是说,它执行软件中的每个汇编指令(除了内核调用之外)。多线程程序被串行化,即一次只有一个执行线程在取得进展。
如果您的应用程序是多线程的,那么当它在 valgrind 竞争条件下执行时,缺乏同步可能会被线程序列化掩盖,从而无法观察到此类错误的影响。
Valgrind runs you program on a virtual CPU, that is, it executes every assembly instruction in software (apart from kernel calls). Multi-threaded programs get serialized, i.e. only one thread of execution is making progress at one time.
If your application is multi-threaded, when it is executed under valgrind race condition and the lack of synchronization may be masked by the thread serialization, so that the effects of such bugs are not observed.