valgrind 和常规 c++ 之间有什么区别?跑步

发布于 2024-11-29 20:21:19 字数 319 浏览 0 评论 0原文

我正在尝试确定 我的代码中有错误,我在其中得到了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

新雨望断虹 2024-12-06 20:21:19

来自 Valgrind 常见问题解答

4.4。我的程序正常崩溃,但在 Valgrind 或 Vice 下不会崩溃
反之亦然。发生什么事了?

当程序在Valgrind下运行时,其环境略有不同
与本机运行时不同。例如,内存布局是
不同,线程的调度方式也不同。

大多数时候这没有任何区别,但它可以,
特别是如果您的程序有错误。例如,如果您的程序
崩溃是因为它错误地访问了不可寻址的内存,
运行时该内存可能不是不可寻址的
在瓦尔格林德的领导下。或者,如果您的程序存在数据争用,这些
可能不会在 Valgrind 下显现。

你无法做任何事情来改变这一点,这只是自然现象
Valgrind 的工作方式是它无法完全复制原生的
执行环境。如果您的程序由于以下原因崩溃
本地运行时出现内存错误,但在 Valgrind 下运行时不会出现内存错误,在
大多数情况下 Memcheck 应该识别出错误的内存操作。

所以你不能与它有任何关系。实际上你不必担心你的程序在 Valgrind 下不会崩溃。您应该从中读取错误消息并修复它们。从无效读取/无效写入错误开始。它们几乎总是指出代码中的错误。在这种特殊情况下,您还可以从简单的 bash 脚本无限循环中运行代码,直到它产生错误消息。您很可能正在使用无效的迭代器,这在 C++ 中是未定义行为

From Valgrind FAQ:

4.4. My program crashes normally, but doesn't under Valgrind, or vice
versa. What's happening?

When a program runs under Valgrind, its environment is slightly
different to when it runs natively. For example, the memory layout is
different, and the way that threads are scheduled is different.

Most of the time this doesn't make any difference, but it can,
particularly if your program is buggy. For example, if your program
crashes because it erroneously accesses memory that is unaddressable,
it's possible that this memory will not be unaddressable when run
under Valgrind. Alternatively, if your program has data races, these
may not manifest under Valgrind.

There isn't anything you can do to change this, it's just the nature
of the way Valgrind works that it cannot exactly replicate a native
execution environment. In the case where your program crashes due to a
memory error when run natively but not when run under Valgrind, in
most cases Memcheck should identify the bad memory operation.

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++.

静赏你的温柔 2024-12-06 20:21:19

也许问题可能与时间相关,当您使用 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.

尹雨沫 2024-12-06 20:21:19

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.

如果没有 2024-12-06 20:21:19

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文