CS253

CS253: Software Development with C++

Spring 2017

Not Fully Specified

See this page as a slide show

Not Fully Specified

What the language definition does not say.

Different types of odd stuff

The C++ standard defines several kinds of not-fully specified things:

Implementation-defined (§1.9.2):

A choice made by the compiler, must be documented

Unspecified behavior (§1.9.3):

A choice made by the compiler, need not be documented

Undefined behavior (§1.9.4):

All bets are off!

Implementation-defined

A choice made by the compiler, must be documented.

Unspecified

A choice made by the compiler, need not be documented or consistent.

// Order of evaluation of an expression (mostly):
int foo() { cout << "foo\n"; return 1; }
int bar() { cout << "bar\n"; return 1; }
int main() {
    cout << foo()+bar() << '\n';
}
foo
bar
2


// Order of evaluation of function arguments:
int foo() { cout << "foo\n"; return 1; }
int bar() { cout << "bar\n"; return 1; }
void ignore_arguments(int, int) { }
int main() {
    ignore_arguments(foo(), bar());
}
bar
foo

Undefined behavior

All bets are off! Anything can happen. Warnings are not required.

int *p = nullptr;
cout << "alpha\n";
cout << *p << '\n';
SIGSEGV: Segmentation fault


int n = 1067316150;
cout << * (float *) &n << '\n';
1.234

int a = 0;
cout << ++a + ++a << '\n';
c.cc:2: warning: operation on 'a' may be undefined
4

But Why?

C++ is quite concerned about efficiency.

C++’s attitude is “You break the rules, you pay the price.” It doesn’t hold your hand.

Modified: 2017-01-28T22:24

User: Guest

Check: HTML CSS
Edit History Source
Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2015 Colorado State University
CS Building