c++程序设计经典
828 Technicalities Appendix Cor bad; it simply says what a programmer can and cannot rely on from an implementation. One canwrite perfectly awful standard-conforming programs, and most real-world programs rely on fea-tures not covered by the standard.
Many important things are deemedimplementation-definedby the standard. This means thateach implementation must provide a specific, well-defined behavior for a construct and that behav-ior must be documented. For example:
u un ns si ig gn ne ed d c ch ha ar r c c1 1=6 64 4; / /well-defined: a char has at least 8 bits and can always hold 64u un ns si ig gn ne ed d c ch ha ar r c c2 2=1 12 25 56 6; / /implementation-defined: truncation if a char has only 8 bits
The initialization ofc c1 1is well-defined because ac ch ha ar rmust be at least 8 bits. However, the behav-ior of the initialization ofc c2 2is implementation-defined because the number of bits in ac ch ha ar risimplementation-defined. If thec ch ha ar rhas only 8 bits, the value1 12 25 56 6will be truncated to2 23 32 2(§C.6.2.1). Most implementation-defined features relate to differences in the hardware used to runa program.
When writing real-world programs, it is usually necessary to rely on implementation-definedbehavior. Such behavior is the price we pay for the ability to operate effectively on a large range ofsystems. For example, the language would have been much simpler if all characters had been 8 bitsand all integers 32 bits. However, 16-bit and 32-bit character sets are not uncommon–nor areintegers too large to fit in 32 bits. For example, many computers now have disks that hold morethat3 32 2G Gbytes, so 48-bit or 64-bit integers can be useful for representing disk addresses.
To maximize portability, it is wise to be explicit about what implementation-defined featureswe rely on and to isolate the more subtle examples in clearly marked sections of a program. A typi-cal example of this practice is to present all dependencies on hardware sizes in the form of con-stants and type definitions in some header file. To support such techniques, the standard libraryprovidesn nu um me er ri ic c_ _l li im mi it ts s(§22.2).
Undefined behavior is nastier. A construct is deemedundefinedby the standard if no reason-able behavior is required by an implementation. Typically, some obvious implementation tech-nique will cause a program using an undefined feature to behave very badly. For example:
c co on ns st t i in nt t s si iz ze e=4 4*1 10 02 24 4;
c ch ha ar r p pa ag ge e[s si iz ze e] ;
v vo oi id d f f()
{
p pa ag ge e[s si iz ze e+s si iz ze e] =7 7; / /undefined
}
Plausible outcomes of this code fragment include overwriting unrelated data and triggering a hard-ware error/exception. An implementation is not required to choose among plausible outcomes.Where powerful optimizers are used, the actual effects of undefined behavior can become quiteunpredictable. If a set of plausible and easily implementable alternatives exist, a feature is deemedimplementation-defined rather than undefined.
It is worth spending considerable time and effort to ensure that a program does not use some-thing deemed undefined by the standard. In many cases, tools exist to help do this.
The C++ Programming Language, Third Editionby Bjarne Stroustrup. Copyright ©1997 by AT&T.
Published by Addison Wesley Longman, Inc. ISBN 0-201-88954-4. All rights reserved.
c++程序设计经典 (2) c++程序设计经典 (3) c++程序设计经典 (4) c++程序设计经典 (5) c++程序设计经典 (7) c++程序设计经典 (8) c++程序设计经典 (9) ...
c++程序设计经典 (1) c++程序设计经典 (2) c++程序设计经典 (3) c++程序设计经典 (4) c++程序设计经典 (5) c++程序设计经典 (7) c++程序设计经典 (8) ...
c++程序设计经典 (1) c++程序设计经典 (2) c++程序设计经典 (3) c++程序设计经典 (4) c++程序设计经典 (5) c++程序设计经典 (7) c++程序设计经典 (8) ...
c++程序设计经典 (2) c++程序设计经典 (3) c++程序设计经典 (4) c++程序设计经典 (5) c++程序设计经典 (7) c++程序设计经典 (8) c++程序设计经典 (9) ...
c++程序设计经典 (1) c++程序设计经典 (2) c++程序设计经典 (4) c++程序设计经典 (5) c++程序设计经典 (7) c++程序设计经典 (8) c++程序设计经典 (9) ...
c++程序设计经典 (2) c++程序设计经典 (3) c++程序设计经典 (4) c++程序设计经典 (5) c++程序设计经典 (7) c++程序设计经典 (8) c++程序设计经典 (9) ...
很经典的课件很经典的课件隐藏>> C++程序设计谭浩强编著 清华大学出版社 课件...2)选择源程序存放的目录和输入源程序名,单击 “确定”。 3)在编辑器中编写源...
c++程序设计经典 (2) c++程序设计经典 (3) c++程序设计经典 (4) c++程序设计经典 (5) c++程序设计经典 (7) c++程序设计经典 (8) c++程序设计经典 (9) ...
c++程序设计经典 (2) c++程序设计经典 (3) c++程序设计经典 (4) c++程序设计经典 (5) c++程序设计经典 (7) c++程序设计经典 (8) c++程序设计经典 (9) ...
c++程序设计经典 (1) c++程序设计经典 (2) c++程序设计经典 (3) c++程序设计经典 (4) c++程序设计经典 (5) c++程序设计经典 (7) c++程序设计经典 (8) ...
热门文档