CS253

CS253: Software Development with C++

Spring 2018

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.

short s=1;
while (s > 0) s++;
s--;
cout << s << '\n';
32767
double d=1.0;
while (d/2 > 0) d/=2;
cout << d << '\n';
4.94066e-324
  • In ASCII, 'i'+1 == 'j'
  • In EBCDIC, 'i'+8 == 'j'

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: In function 'int main()':
c.cc:2:9: 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: 2018-01-07T15:05

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