Skip to main content
deleted 8 characters in body
Source Link
thb
  • 14.4k
  • 12
  • 11

First, I agree with everything @Leushenko wrote. I wish however to add something to it.

I feel strongly that undefined behavior in C and C++ should be explained, not merely ruled out. There are at least three, distinct reasons:

  1. The very reason one uses C or C++ rather than, say, Java, is to program close to hardware. The algorithms may be similar in C/C++ and Java, but the mindset is very different. Programming Java, one might think of the physical machine as an incidental agent whose role it is to run the code. Programming C or C++ however, one might think of the compiler, and even the code it compiles, as incidental agents whose purpose it is to marshal and deploy machine instructions to the hardware. The one mindset is code- and algo-centric; the other is comparatively silicon-centric. (There are many non-silicon-centric C++ programmers, of course; but more than a few of these find C++ frustrating, and wish that they were programming in some other language. The silicon-centric programmer finds C++ liberating, which is my point.)

  2. C and C++ rule the important domain of embedded systems, in which the programming often really is about the hardware itself, about which the C and C++ standards speak in only a general way.

  3. Even if one would never ship code that purposely relied on undefined behavior, a deep understanding of undefined behavior is most useful during testing and debugging.

Undefined behavior is of course usually undesirable in C and C++ but, technically, it merely describes operations whose effects simply lie outside outside the compiler's purview. Consider: every kernel system call invokes undefined behavior!

Not all C/C++ programs call, or should call, the operating-system kernel; but more than a few do. Such programs have limited interest in portability, and are happy to be programmed in languages like C and C++.

Sometimes our answers on StackOverflow inadvertently leave the impression that undefined behavior were more or less arbitrary. Of course, it really isn't very arbitrary in most cases, but rather tends to be somewhat predictable if you understand stack pointers, memory managers, and the essential differences between machine architectures. Usually, a programmer does not want to worry about such things—except when he does. At any rate, the reasons underlying specific undefined behavior are often profitable for the C or C++ programmer to know, even if the programmer ought to avoid provoking undefined behavior in shipped code.

First, I agree with everything @Leushenko wrote. I wish however to add something to it.

I feel strongly that undefined behavior in C and C++ should be explained, not merely ruled out. There are at least three, distinct reasons:

  1. The very reason one uses C or C++ rather than, say, Java, is to program close to hardware. The algorithms may be similar in C/C++ and Java, but the mindset is very different. Programming Java, one might think of the physical machine as an incidental agent whose role it is to run the code. Programming C or C++ however, one might think of the compiler, and even the code it compiles, as incidental agents whose purpose it is to marshal and deploy machine instructions to the hardware. The one mindset is code- and algo-centric; the other is comparatively silicon-centric. (There are many non-silicon-centric C++ programmers, of course; but more than a few of these find C++ frustrating, and wish that they were programming in some other language. The silicon-centric programmer finds C++ liberating, which is my point.)

  2. C and C++ rule the important domain of embedded systems, in which the programming often really is about the hardware itself, about which the C and C++ standards speak in only a general way.

  3. Even if one would never ship code that purposely relied on undefined behavior, a deep understanding of undefined behavior is most useful during testing and debugging.

Undefined behavior is of course usually undesirable in C and C++ but, technically, it merely describes operations whose effects simply lie outside outside the compiler's purview. Consider: every kernel system call invokes undefined behavior!

Not all C/C++ programs call, or should call, the operating-system kernel; but more than a few do. Such programs have limited interest in portability, and are happy to be programmed in languages like C and C++.

Sometimes our answers on StackOverflow inadvertently leave the impression that undefined behavior were more or less arbitrary. Of course, it really isn't very arbitrary in most cases, but rather tends to be somewhat predictable if you understand stack pointers, memory managers, and the essential differences between machine architectures. Usually, a programmer does not want to worry about such things—except when he does. At any rate, the reasons underlying specific undefined behavior are often profitable for the C or C++ programmer to know, even if the programmer ought to avoid provoking undefined behavior in shipped code.

First, I agree with everything @Leushenko wrote. I wish however to add something to it.

I feel strongly that undefined behavior in C and C++ should be explained, not merely ruled out. There are at least three, distinct reasons:

  1. The very reason one uses C or C++ rather than, say, Java, is to program close to hardware. The algorithms may be similar in C/C++ and Java, but the mindset is very different. Programming Java, one might think of the physical machine as an incidental agent whose role it is to run the code. Programming C or C++ however, one might think of the compiler, and even the code it compiles, as incidental agents whose purpose it is to marshal and deploy machine instructions to the hardware. The one mindset is code- and algo-centric; the other is comparatively silicon-centric. (There are many non-silicon-centric C++ programmers, of course; but more than a few of these find C++ frustrating, and wish that they were programming in some other language. The silicon-centric programmer finds C++ liberating, which is my point.)

  2. C and C++ rule the important domain of embedded systems, in which the programming often really is about the hardware itself, about which the C and C++ standards speak in only a general way.

  3. Even if one would never ship code that purposely relied on undefined behavior, a deep understanding of undefined behavior is most useful during testing and debugging.

Undefined behavior is of course usually undesirable in C and C++ but, technically, it merely describes operations whose effects simply lie outside the compiler's purview. Consider: every kernel system call invokes undefined behavior!

Not all C/C++ programs call, or should call, the operating-system kernel; but more than a few do. Such programs have limited interest in portability, and are happy to be programmed in languages like C and C++.

Sometimes our answers on StackOverflow inadvertently leave the impression that undefined behavior were more or less arbitrary. Of course, it really isn't very arbitrary in most cases, but rather tends to be somewhat predictable if you understand stack pointers, memory managers, and the essential differences between machine architectures. Usually, a programmer does not want to worry about such things—except when he does. At any rate, the reasons underlying specific undefined behavior are often profitable for the C or C++ programmer to know, even if the programmer ought to avoid provoking undefined behavior in shipped code.

Source Link
thb
  • 14.4k
  • 12
  • 11

First, I agree with everything @Leushenko wrote. I wish however to add something to it.

I feel strongly that undefined behavior in C and C++ should be explained, not merely ruled out. There are at least three, distinct reasons:

  1. The very reason one uses C or C++ rather than, say, Java, is to program close to hardware. The algorithms may be similar in C/C++ and Java, but the mindset is very different. Programming Java, one might think of the physical machine as an incidental agent whose role it is to run the code. Programming C or C++ however, one might think of the compiler, and even the code it compiles, as incidental agents whose purpose it is to marshal and deploy machine instructions to the hardware. The one mindset is code- and algo-centric; the other is comparatively silicon-centric. (There are many non-silicon-centric C++ programmers, of course; but more than a few of these find C++ frustrating, and wish that they were programming in some other language. The silicon-centric programmer finds C++ liberating, which is my point.)

  2. C and C++ rule the important domain of embedded systems, in which the programming often really is about the hardware itself, about which the C and C++ standards speak in only a general way.

  3. Even if one would never ship code that purposely relied on undefined behavior, a deep understanding of undefined behavior is most useful during testing and debugging.

Undefined behavior is of course usually undesirable in C and C++ but, technically, it merely describes operations whose effects simply lie outside outside the compiler's purview. Consider: every kernel system call invokes undefined behavior!

Not all C/C++ programs call, or should call, the operating-system kernel; but more than a few do. Such programs have limited interest in portability, and are happy to be programmed in languages like C and C++.

Sometimes our answers on StackOverflow inadvertently leave the impression that undefined behavior were more or less arbitrary. Of course, it really isn't very arbitrary in most cases, but rather tends to be somewhat predictable if you understand stack pointers, memory managers, and the essential differences between machine architectures. Usually, a programmer does not want to worry about such things—except when he does. At any rate, the reasons underlying specific undefined behavior are often profitable for the C or C++ programmer to know, even if the programmer ought to avoid provoking undefined behavior in shipped code.