Is undefined behavior an omission?

The standard says:

  1.3.24 [defns.undefined]
 undefined behavior
 behavior for which this International Standard imposes no requirements
 [Note: Undefined behavior may be expected when this International Standard 
 omits any explicit definition of behavior or when a program uses an erroneous 
 construct or erroneous data.  Permissible undefined behavior ranges from ignoring 
 the situation completely with unpredictable results, to behaving during 
 translation or program execution in a documented manner characteristic of the 
 environment (with or without the issuance of a diagnostic message), to 
 terminating a translation or execution (with the issuance of a diagnostic 
 message).  Many erroneous program constructs do not engender undefined behavior; 
 they are required to be diagnosed.
 - end note] 

Obviously, the standard cannot cover all possible aspects of behavior. Therefore, it seems that if something is not covered by the standard, it may be undefined. What exactly does this mean?

He implied that if a standard does not cover anything, it cannot impose requirements on it. However, how can a standard say something like undefined behavior without saying it explicitly? Literally, something can happen in the program, and it is assumed that this is not undefined behavior unless specified by the standard. Does this mean that by default a non-standard program has undefined behavior?


The respondent says the notes are non-normative. Answers to this Are notes and examples in the specification of the main language of the standard non-standard standard C ++? which says:

The notes and examples included in the text of the document should be used to provide additional information designed to help understand or use the document. They should not contain requirements (“must”, see 3.3.1 and table H.1) or any information is considered indispensable for using the document, for example. instructions (required, see table H.1), recommendations (“should”; see 3.3.2 and table H.2) or authorization (“may”, see table H.3). Notes can be written as a statement of fact.

It seems to me that something that is omitted is not strictly undefined behavior. For example, is this something strict undefined behavior if it is mentioned in a note?

+6
source share
3 answers

However, how can the standard say something, is this behavior undefined without saying it explicitly?

Because it means undefined . What is happening is not defined. The standard defines that expected from a valid program, he does not try to list every possible invalid program and say "it is undefined, also it is undefined, also it is undefined".

The standard does not indicate what happens if you fire on a computer while the program is running. This does not mean that it is clearly defined. This is clearly undefined.

Literally anything can happen in the program, and it is assumed that this is not undefined behavior unless specified by the standard.

I'm not sure what you are trying to say here, but it sounds 180 ° back.

Does this mean that by default a non-standard program has undefined behavior?

What is a custom program?

+6
source

The last sentence of the note you indicated explains may in its first sentence:

Many erroneous software constructs do not generate undefined behavior; they must be diagnosed.

Thus, it can be quite correct.

Everything that is not defined is undefined, although there are many ways to define it (even if it is only slightly limited).

In addition, since notes are not normative, they cannot determine behavior.

+2
source

Formally, everything that the standard does not define is undefined. How is this determined if the standard does not define it?

In practice, there are several things that are accepted as given; if the standard defines behavior, for example, the implementation cannot add additional observable behavior. (That is, in an expression like a = 2 * a; implementation is not allowed to change anything other than a , although I don’t think you will find an explicit transition to this effect in the standard.)

But about that. If the standard does not define it, undefined according to the standard. (An implementation may give additional definitions. In fact, all do.)

+1
source

Source: https://habr.com/ru/post/977121/


All Articles