c++11 - "Random" output from a determinstic C++ program. Possible causes? -


i'm using microsoft visual studio community 2015, version 14.xxx on 64-bit windows pc.

the program reads text file in each line bridge deal (four players each 13 cards). file generated 3rd party program reliable validate each deal anyway , each input line passes validation.

i group "similar" deals classes (bins) based on variables such number of points, suit length etc. standard text processing using stringstreams , map of bin structure. it's totally deterministic given input file.

about 3/4 of time same output e.g. 23 possible bins - , frequency of deals across bins adds number of input deals, expected. remaining output might have 6 50 bins (with correct frequency totals).

where might such randomness arise? use default initialization of variables so, if wrong, should consistent across program runs on given file. example,

std::string line;  //  raw data on deal. std::vector<std::string> parsed_deal; std::map<std::string, struct bin> m_bin; std::stringstream ss_bin[max_bins];  

default initialisation not mean variables initialised in same way, every time program run. in particular, default initialisation means uninitialised in several circumstances. examples include basic types (int, float, pointers, arrays of them, etc) being uninitialised (e.g. auto variables not initialised zero), , such member of class type not initialised constructor. accessing value of uninitialised variable (let alone dereferencing if pointer) gives undefined behaviour.

there are, naturally, other causes of undefined behaviour (falling off end of array, molesting pointer, invoking operator delete twice on same pointer, invoking operator delete on pointer returned malloc()).

if behaviour undefined, "non-deterministic" behaviour 1 possible outcome. example, when variable defined not initialised, value may based on whatever happened in physical/logical memory location previously. result of accessing value depend on other code (in program, in operating system, in process) using memory previously, , how/if memory overwritten before program accessed it.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -