-2

While C++ has forward progress guarantees, I don't see any analogous rule in the C11 specification.

Would a C11 compiler that emitted an infinite loop for every function conform to the C11 specification? Assume that it does all preprocessing / etc as usual; it just has a rather overzealous optimizer.

Admittedly, this would be a rather poor compiler. Nevertheless.

TLW
  • 1,373
  • 9
  • 22

1 Answers1

0

The Standard's One Program Rule allows implementations to impose arbitrary "translation limits" the designers see fit, and behave in arbitrary fashion if they are violated, provided only that for every translation limit given in the Standard there exists at least One Program which the implementation processes correctly. An implementation may do anything it likes when given any other program.

Further, the Standard only concerns itself with program behavior, and not with generated code, and consequently makes no practical distinction between a program that gets stuck in an endless loop, and one which would run for trillions of years with no side effects before producing a result.

In general, the proper answer to the question "Would the C Standard allow a compiler to behave in some stupid and useless fashion X" is "Probably. So?" The Standard makes no effort to forbid compilers from doing stupid things, and the fact that the Standard would allow a conforming implementation to do something implies no judgment as to whether such behavior might make an implementation useless for some (or even all) purposes.

supercat
  • 77,689
  • 9
  • 166
  • 211
  • I'm asking about a compiler that turns _every_ function into while 1; you can assume that that includes functions that do not exceed the translation limits. Note that C11 raised said translation limits. – TLW Mar 18 '19 at 00:14
  • Assuming your second paragraph is true, this is an interesting and significant difference between C and C++, as C++ guarantees forward progress in many cases. – TLW Mar 18 '19 at 00:22
  • @TLW: The translation limits in C11 are only meaningful "One Program". Provided that an implementation correctly processes at least one program that exercises each translation limit, it can do anything it likes with any other program. The authors of the Standard recognize that it would be possible to contrive an implementation that is simultaneously conforming and useless, but expect that compiler writers will opt to produce quality implementations instead. BTW, the increased object size and change to PTRDIFF_MAX was silly. An implementation for a large machine... – supercat Mar 18 '19 at 02:20
  • ...should be able to handle objects much larger than 64K, but on a freestanding implementation whose *total* storage is less than 32K, there's no reason ptrdiff_t should need to be larger than 16 bits. – supercat Mar 18 '19 at 02:21