非决定论的来源

发布于 2024-09-16 20:38:55 字数 1435 浏览 6 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(12

一江春梦 2024-09-23 20:38:56

你没有提供很多信息。然而,作为一个以实时编程为生的人,当这种事情发生时,我寻找的最有可能的罪魁祸首是:

  • 使用未初始化的内存。
  • 竞争条件。
  • 上述一些模糊的组合。

例如,我曾经遇到过一个这样的麻烦,因为共享库并不像我想象的那样“共享”,并且试图使用一个进程的句柄来索引第二个进程中尚未初始化的表。根据事情的启动方式,可能会或可能不会导致第三个进程中的重要数据被丢弃。

You didn't give a lot of info. However, as someone who does real-time programming for a living the most likely culprits I look for when such things happen is:

  • Using uninitialized memory.
  • A race condition.
  • Some obscure combination of the above.

For instance, one such trouble I had once came down to shared library not being as "shared" as I thought, and trying to use a handle from one process to index a table that was as yet uninitialized in a second process. Depending on how things started up that may or may not have caused important data in yet a third process to get trashed.

野生奥特曼 2024-09-23 20:38:56

任何未定义的行为。即:需要数百页的时间来解释输出的每一个可能的改变来源。尝试调试以查找发生更改的位置,或阅读一些 C++ 规范。

Any undefined behavior. i.e: it will take hundreds of pages to explain every possible source of alteration of the output. Try debugging to find where alteration occurs, or reading some C++ spec.

心作怪 2024-09-23 20:38:55

有多种方式:

  • 以涉及数据竞争的方式使用多个线程,
  • 使用当前系统时间作为输入,
  • 使用未初始化的变量,
  • ...

我们当然可以做出更多猜测,但如果您想获得有意义的帮助,也许发布代码的相关部分会更好:-)

In several ways:

  • using multiple threads in a way that involves a data race,
  • using the current system time as input,
  • using uninitialized variables,
  • ...

We can surely make more guesses, but if you want to get meaningful help, maybe it would be good for you to publish the relevant parts of your code :-)

我的影子我的梦 2024-09-23 20:38:55

如果您的输出取决于堆上分配的地址:

int main(int argc, char* argv[])
{
   printf("%p", malloc(42));
   return 0;
}

对于每次运行,malloc() 可能会返回不同的虚拟地址 - 更不用说分配失败时的 NULL。

If your output depends on an address allocated on the heap:

int main(int argc, char* argv[])
{
   printf("%p", malloc(42));
   return 0;
}

For each run, the malloc() may return a different virtual address - not to mention NULL in case the allocation failed.

雨落星ぅ辰 2024-09-23 20:38:55

它可能是:

  • 线程计时
  • 任何类型的输入(用户、文件、网络等)

It could be:

  • Thread timing
  • Any kind of input (user, file, network, etc)
燃情 2024-09-23 20:38:55

如果您的程序使用 float / double,如果某些架构上存在上下文切换,结果可能会有所不同。

在 x86 上,FPU 对中间结果使用扩展精度,但是当保存在内存中时(当进程或线程存在上下文切换时会发生这种情况),这种精度就会丢失。这可能会导致结果出现一些小偏差(我们在程序中检测到了此类问题)。避免此问题的一种方法是要求编译器不要使用 FPU 而使用 SSE 进行浮点运算。

http://www.network-theory.co.uk/docs/ gccintro/gccintro_70.html

If your program use float / double, there may be difference in the result if there are context switch on some architecture.

On x86, the FPU use extended precision for intermediary result, but when saved in memory (which happens when there is a context switch either process or thread), such precision is lost. That could cause some small divergence of the result (we've detected such problem in our program). One way to avoid this issue is to ask the compiler not to use FPU but SSE for floating point operations.

http://www.network-theory.co.uk/docs/gccintro/gccintro_70.html

余生一个溪 2024-09-23 20:38:55

除了对 rand() 的杂散调用

只要您向它提供相同的初始种子,rand() 就完全是确定性的。

Besides a stray call to rand()

rand() is entirely deterministic as long as you feed it the same initial seed.

滥情稳全场 2024-09-23 20:38:55

如果没有看到一些代码(提示提示),我能想到的最好的办法就是寻找一种模式。也许是特定于日期时间的东西。

另外,尝试寻找竞争条件。这看起来是不确定的。

Without seeing some code (HINT HINT), the best I can think of would be looking for a pattern. Maybe something date-time specific.

Also, try looking for race conditions. That can look non-deterministic.

梦与时光遇 2024-09-23 20:38:55

使用指针的值而不是它所指向的值总是会产生有趣的结果。

Using the value of a pointer instead of what it points to always produces interesting results.

本王不退位尔等都是臣 2024-09-23 20:38:55

在与“外部世界”交互不多的程序中,非确定性的流行来源是对指针比较的依赖。有时您可能会在代码中看到它:当字典比较函数用完要比较的内容时(一切都相等),它会比较对象的地址作为最后的手段。如果在动态内存中分配对象,这可能会产生不同的顺序,因为实际的分配位置可能因平台和运行而异。

In the programs that don't interact much with the "outside world" the popular source of non-determinism is the reliance on pointer comparison. From time to time you might see it in the code: when a lexicographical comparison function runs out of things to compare (everything is equal) it compares the addresses of objects as the last resort. This can produce different orderings if the objects are allocated in the dynamic memory, since the actual allocation locations can differ from platform to platform and from run to run.

萌化 2024-09-23 20:38:55

显然是月相错误的一个新实例。

Obviously a new instance of the Phase of the Moon bug.

我偏爱纯白色 2024-09-23 20:38:55
  • 来自网络/互联网的输入。
  • 日期/时间
  • Inputs from network / internet.
  • date / time
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文