Is the behavior defined by the implementation consistent between runs in C ++?

Is a standard compatible C ++ implementation allowed to implement any behavior that is said to be defined in the standard in the standard in such a way that it differs from different runs of the same compiled one-time program with the same input?

For example, is this an implementation that allows you to say "behavior is on weekends, otherwise" and implement the behavior in accordance with this statement?

+7
c ++ compiler-construction implementation
source share
6 answers

Of course, if the implementation documents, when exactly the behavior changes with different runs, this is fine. Note that the behavior defined by the implementation is part of the parameters of the abstract machine:

The semantic descriptions in this International Standard define a parameterized non-deterministic abstract machine.

Some aspects and operations of an abstract machine are described in this International Standard as an implementation (for example, sizeof (int)). They make up the parameters of an abstract machine. Each implementation should include documentation describing its characteristics and behavior in these relationships. Such documentation should define an instance of the abstract machine that corresponds to this implementation (referred to as the “corresponding instance below).

This does not allow changing behavior in a single compiler run. But between different runs of the compiler, the compiler can use another corresponding abstract machine, which differs in different values ​​of the implementation, in accordance with what is defined by the implementation. The most common examples of this are command line options, such as -Wall (which modifies a specific set of diagnostic messages). This is in contrast to unspecified behavior in addition to documentation requirements. Undefined behavior is much less restrictive:

Some other aspects and operations of an abstract machine are described in this International Standard as unspecified (for example, the order in which function arguments are evaluated). Where possible, this International Standard defines a set of acceptable behaviors. They define the non-deterministic aspects of an abstract machine. Thus, an abstract machine instance can have more than one possible execution sequence for a given program and a given input.

+6
source share

One example that I can think of is that a program uses a large or small representation for numbers. I believe that this will certainly be considered as behavior defined by the implementation.

On some chips, for example, on some ARM chips, you can switch modes at runtime, so you may need a compiler that can create a program that will work in any mode, which means that you have a specific implementation that could potentially differ from each of them is performed depending on external settings.

Similarly, I think you could write a compiler that prepared a 32-bit 64-bit bit compiled from the same program - and the mode that it executed could be determined at runtime. Again, the documentation would have to say that the ints were 32 bits or 64 bits depending on how you ran it.

Honestly, I don’t see anyone doing any of these things, but they both sound vaguely plausible examples of what you asked for, and I don’t understand why they will not be legal by standard, as long as the documentation has correctly documented System-specific behavior.

+3
source share

Behavioral implementation means

Undefined behavior in which each implementation documents how choices are made

Mandatory for compiler authors is to document the behavior of a particular programming construct for a particular implementation.

..... so that it differs from different runs of the same compiled one-time program with the same input?

Nopes!

For example, is this an implementation that allows you to say "behavior is on weekends, otherwise" and implement the behavior in accordance with this statement?

I'm not sure, but I think the answer is no .

+1
source share

IIRC, system () is required for existence, but subject to the implementation of certain behavior. Something like system ("ls | grep foo") will naturally have different effects, based on whether your system can do something called ls, which can vary depending on the runs. And even on a fairly normal UNIX machine, where ls and grep do what you expect and don’t take out, the result will still depend on the existence of a file named foo in the name, which, of course, will be allowed to change the time and where a program is running, etc. It just depends on where you draw the string "same input". If the machine is in a completely identical state, then you can expect the same behavior, but no two runs will turn on the machine in a truly pedantic identical state. (Even the temperature of the CPU in completely identical machines can lead to some choke, which changes the winner of a certain race condition, which noticeably leads to different behavior of your program.)

+1
source share

Warranties are what the compiler has documented. Different compiler flags or a different computer state during compilation may affect how the compiler / optimizer processes your program and what may affect the result. With maximum consequences for the compiler (the same compiler can be used to generate 32 and 64-bit programs in a 64-bit environment, alignment requirements may differ in two scenarios).

You can expect that in most cases, the developer will provide some basic guarantees regarding the implementation behavior and the program that he creates for a given set of compiler / linker parameters. Even if the system load can affect how optimizer can work in your program, some optimizers will be allocated for a limited time - this should not change the expected behavior.

Note that while there is no guarantee, it would be difficult to sell a compiler that generates code with different behavior depending on unrelated parameters like the position of the moon relative to the stars.

+1
source share

rand(3) in <stdlib.h> can be called from C ++, I'm not sure that most of the C library is included in "standard C ++", but, of course, there is some standard that corresponds to the random number generation mechanism.

time(3) in <time.h> returns the current time; the same story with C ++ and a call to the C library.

0
source share

All Articles