Is it possible for code that meets the following conditions to produce different outputs for each run for the same input?
- The code is single threaded, though
it does link against a thread-safe
runtime library. - There are no explicit calls to rand() or time() or their friends.
- There are some heap memory allocations.
- There might be some (buggy) code which results in undefined behavior.
“Undefined behavior” means that anything can happen. This also includes that different things might happen on each run of the program.
For example, if you use uninitialized memory it might be different from program run to program run what exactly that memory contains.
A simple example:
This will usually print a different string each time it is run. It doesn’t use any heap allocations and I don’t think it’s even any undefined behavior, so probably it’s not the intended solution to your question.
Another example would be that
newcan return different addresses on each program run (also no UB here):So even without undefined behavior there are sources of “randomness”, so certainly also with undefined behavior different things can happen each program run.