简体   繁体   English

确定性C ++程序的“随机”输出。 可能的原因?

[英]“Random” output from a determinstic C++ program. Possible causes?

I'm using Microsoft Visual Studio Community 2015, Version 14.xxx on a 64-bit Windows PC. 我在64位Windows PC上使用Microsoft Visual Studio Community 2015,版本14.xxx。

The program reads a text file in which each line is a Bridge deal (four players each with 13 cards). 该程序读取一个文本文件,其中每一行都是Bridge交易(四个玩家每人拥有13张卡)。 The file is generated by a 3rd party program that is reliable but I validate each deal anyway and each input line passes the validation. 该文件由可靠的第三方程序生成,但无论如何我都会验证每个交易,并且每个输入行都会通过验证。

I then group "similar" deals together into classes (bins) based on variables such as number of points, suit length etc. This is standard text processing using stringstreams and a map of my Bin structure. 然后,我根据变量(例如点数,衣服长度等)将“相似”交易分组到类(箱)中。这是使用字符串流和Bin结构图的标准文本处理。 It's totally deterministic for a given input file. 对于给定的输入文件,这完全是确定性的。

About 3/4 of the time I get the same output eg 23 possible bins - and the frequency of deals across the bins adds up to the number of input deals, as expected. 大约有3/4的时间,我得到了相同的输出,例如23个可能的仓位-像预期的那样,仓位中交易的频率总计等于输入交易的数量。 But the remaining output might have anything from 6 to 50 bins (with correct frequency totals). 但是剩余的输出可能有6到50个bin的任何东西(频率总和正确)。

Where might such randomness arise? 这种随机性会在哪里出现? I use default initialization of all variables so, even if that were wrong, it should be consistent across program runs on a given file. 我使用所有变量的默认初始化,因此,即使那是错误的,它也应在给定文件上的程序运行中保持一致。 For example, 例如,

std::string line;  //  Raw data on a deal.
std::vector<std::string> parsed_deal;
std::map<std::string, struct Bin> m_bin;
std::stringstream ss_bin[MAX_BINS]; 

Default initialisation does not mean that all variables will be initialised in exactly the same way, every time your program is run. 默认初始化并不意味着每次运行程序时都将以完全相同的方式初始化所有变量。 In particular, default initialisation means uninitialised in several circumstances. 特别地,默认初始化意味着在某些情况下未初始化。 Examples include basic types ( int , float , pointers, arrays of them, etc) being uninitialised (eg auto variables which are NOT initialised to zero), and such a member of a class type that is not actually initialised by a constructor. 示例包括未初始化的基本类型( intfloat ,指针,它们的数组等)(例如,未初始化为零的auto变量),以及此类类类型的成员实际上并未由构造函数初始化的成员。 Accessing the value of an uninitialised variable (let alone dereferencing it if it is a pointer) gives undefined behaviour. 访问未初始化变量的值(如果它是指针,更不用说对其进行引用)会产生未定义的行为。

There are, naturally, other causes of undefined behaviour (falling off the end of an array, molesting a pointer, invoking operator delete twice on the same pointer, invoking operator delete on a pointer returned by malloc() ). 有,自然地,未定义行为其他原因(脱落的阵列的端部,猥亵指针,调用操作者delete对同一指针两次,调用操作delete由一个返回指针malloc()

If the behaviour is undefined, then "non-deterministic" behaviour is one possible outcome. 如果行为是不确定的,那么“不确定性”行为就是一种可能的结果。 For example, when a variable is defined but not initialised, its value may be based on whatever happened to be in that physical/logical memory location previously. 例如,当定义但未初始化变量时,其值可能基于先前在该物理/逻辑存储位置中发生的任何事情。 So the result of accessing its value will depend on what other code (in your program, in your operating system, even in another process) was using that memory previously, and how/if that memory was overwritten before your program accessed it. 因此,访问它的值的结果将取决于先前有哪些其他代码(在您的程序,操作系统,甚至在另一个进程中)正在使用该内存,以及在程序访问该内存之前如何/是否覆盖了该内存。

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

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