学习笔记(C++Primer)--易错点总结(Chapter2)

2.1.2Type Conversions(1/10/2017)

1.If we assign an out-of-range value to an object of unsigned type, the result is

the remainder of the value modulo
the number of values the target type can

hold. For example, an 8-bit unsigned char can hold values from 0 through

255, inclusive. If we assign a
value outside this range, the compiler assigns the

remainder of that value modulo 256.
Therefore, assigning –1 to an 8-bit

unsigned char gives that object the value 255.

2. If
we assign an out-of-range value to an object of signed type, the result is

undefined.
The program might appear to work, it might crash, or it might

produce
garbage values.

3.Advice:
Avoid Undefined and Implementation-Defined Behavior

Undefined
behavior results from errors that the compiler is not required (and

sometimes
is not able) to detect. Even if the code compiles, a program that

executes
an undefined expression is in error.

Unfortunately,
programs that contain undefined behavior can appear to

execute
correctly in some circumstances and/or on some compilers. There is

no
guarantee that the same program, compiled under a different compiler or

even
a subsequent release of the same compiler, will continue to run

correctly.
Nor is there any guarantee that what works with one set of inputs

will
work with another.

Similarly,
programs usually should avoid implementation-defined behavior,

such
as assuming that the size of an int is a fixed and known value. Such

programs
are said to be nonportable. When the program is moved to another

machine,
code that relied on implementation-defined behavior may fail.

Tracking
down these sorts of problems in previously working programs is,

mildly
put, unpleasant.

4. The
compiler applies these same type conversions when we use a value of one

arithmetic
type where a value of another arithmetic type is expected. For example,

when
we use a nonbool value as a condition (§ 1.4.1, p. 12), the arithmetic value is

converted
to bool in the same way that it would be converted if we had assigned

that
arithmetic value to a bool variable:

int i = 42;

if (i) //
condition will evaluate as true

i = 0;

If
the value is 0, then the condition is false; all other (nonzero) values yield
true.

By
the same token, when we use a bool in an arithmetic expression, its value

always
converts to either 0 or 1. As a result, using a bool in an arithmetic
expression

is
almost surely incorrect.

5. Caution:
Don’t Mix Signed and Unsigned Types

Expressions
that mix signed and unsigned values can yield surprising results

when
the signed value is negative. It is essential to remember that signed

values
are automatically converted to unsigned. For example, in an

expression
like a * b, if a is -1 and b is 1, then if both a and b are ints,

the
value is, as expected -1. However, if a is int and b is an unsigned,

then
the value of this expression depends on how many bits an int has on

the
particular machine. On our machine, this expression yields 4294967295

6.2的32次方是4294967296

7.

unsigned
u = 10, u2 = 42;

std::cout
<< u2 - u << std::endl;//32

std::cout
<< u - u2 << std::endl;//4294967264

int
i = 10, i2 = 42;

std::cout
<< i2 - i << std::endl;//32

std::cout
<< i - i2 << std::endl;//-32

std::cout
<< i - u << std::endl;//0

std::cout <<
u - i << std::endl;//0

2.1.3literals

1.

Although
integer literals may be stored in signed types, technically speaking, the

value
of a decimal literal is never a negative number. If we write what appears to be

a
negative decimal literal, for example, -42, the minus sign is not part of the
literal.

The
minus sign is an operator that negates the value of its (literal) operand.

2.

3. The
type of a string literal is array of constant chars, a type we’ll discuss in §
3.5.4

(p.
122). The compiler appends a null character (’\0’) to every string literal.
Thus, the

actual
size of a string literal is one more than its apparent size. For example, the

literal
‘A‘ represents the single character A, whereas the string literal "A"
represents

an
array of two characters, the letter A and the null character.

4. Two
string literals that appear adjacent to one another and that are separated only

by
spaces, tabs, or newlines are concatenated into a single literal. We use this
form of

literal
when we need to write a literal that would otherwise be too large to fit

comfortably
on a single line:

// multiline
string literal

std::cout <<
"a really, really long string literal "

"that spans
two lines" << std::endl;

时间: 2024-12-20 12:17:21

学习笔记(C++Primer)--易错点总结(Chapter2)的相关文章

学习笔记 UpdateXml() MYSQL显错注入

在学习之前,需要先了解 UpdateXml() . UPDATEXML (XML_document, XPath_string, new_value); 第一个参数:XML_document是String格式,为XML文档对象的名称,文中为Doc 第二个参数:XPath_string (Xpath格式的字符串) ,如果不了解Xpath语法,可以在网上查找教程. 第三个参数:new_value,String格式,替换查找到的符合条件的数据 作用:改变文档中符合条件的节点的值 然后咱们再看看语句:

python学习笔记-import utils报错

今天遇到一个坑爹的问题,查找了半天原因,终于解决了,在此特地记录一下. 运行环境:Windows eclipse 我在eclipse中配置了python的运行环境,在eclipse中编写python代码. 操作步骤: 1.在python交互命令行中,输入import utils,不报错: 2.在eclipse的pydev Project中,输入import utils,报错:Unresolved import:utils 解决方法: 1.eclipse中,点击Window-preferences

Java学习笔记之Scanner报错java.util.NoSuchElementException

转载自:IT学习者-螃蟹 一个方法A使用了Scanner,在里面把它关闭了.然后又在方法B里调用方法A之后就不能再用Scanner了Scanner in = new Scanner(System.in); 测试代码如下: import java.util.Scanner; /** * * @author IT学习者-螃蟹 * * */ public class ItxxzScanner { //第一次输入 public void FistTime (){ Scanner sc = new Sca

C++ Primer 第五版学习笔记

<C++ Primer>第五版中文版学习笔记 ? C++ Primer 第五版学习笔记

C++ Primer 学习笔记_90_用于大型程序的工具 --异常处理[续3]

用于大型程序的工具 --异常处理[续3] 九.auto_ptr类[接上] 5.auto_ptr对象的复制和赋值是破坏性操作 auto_ptr和内置指针对待复制和赋值有非常关键的区别.当复制auto_ptr对象或者将它的值赋给其他auto_ptr对象的时候,将基础对象的所有权从原来的auto_ptr对象转给副本,原来的auto_ptr对象重置为未绑定状态. auto_ptr<string> strPtr1(new string("HELLO!")); auto_ptr<

C++Primer第5版学习笔记(一)

C++Primer第5版学习笔记(一) 第一.二章的重难点内容 本篇文章主要记录了我在学习C++Primer(第5版,中文版)中遇到的重难点及其分析.因为第一.二章比较简单,因此这里合并这两章我遇到的问题.        第一章 开始 这一章在第一部分之前,是一个helloworld式的章节,包含基本的函数,io流以及类的介绍. 知识点1:P19,1.5,文件重定向 可以在windows下的cmd中或者mac,linux系统的终端窗口中用输入命令的形式执行程序并使它从一个文件中读入数据,再把标准

C++Primer学习笔记《三》

数组名其实就是一个常指针,指向数组元素中第一个的地址,在程序中如果要用指针遍历数组,不能直接用数组名来自增或自减,因为它是常量,一般先把数组名保存一份同类型的指针,然后再用这个指针来自增或是自减来实现遍历. 指针也是可以进行算术加法和减法的,但必须保证原地址和结果地址都是想同一个数组的元素或是指向数组的末端元素的下一个单元(类似end()). 指针的减法操作是有意义的,它表示两个指针的相对位置关系,减法结果是ptrdiff_t类型,和size_t类型一样,是一种机器相关的类型,size_t是无符

《C++ Primer Plus》学习笔记9

<C++ Primer Plus>学习笔记9 第15章 友元.异常和其他 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

C++Primer学习笔记《2》

数组是一种复合类型,由类型名+数组名+维度组成.数组定义中的类型可以是C++基本内置类型,也可以是类类型的.数组元素的类型可以是除了引用类型以外的其他任何类型,没有所有的元素都是引用的数组. 数组的维度必须是大于或等于1的编译器常量,所谓的编译器常量就是指在编译期间就可算出结果的表达式,字面值常量,枚举类型,用常量表达式初始化的const对象.任何在编译器无法计算出的值都不可作为数组的维度值. 不管数组在哪里定义,如果其元素是类类型的,则自动调用类的默认构造函数进行初始化,如果该类没有默认的构造

C++ Primer 学习笔记_98_特殊工具与技术 --优化内存分配

特殊工具与技术 --优化内存分配 引言: C++的内存分配是一种类型化操作:new为特定类型分配内存,并在新分配的内存中构造该类型的一个对象.new表达式自动运行合适的构造函数来初始化每个动态分配的类类型对象. new基于每个对象分配内存的事实可能会对某些类强加不可接受的运行时开销,这样的类可能需要使用用户级的类类型对象分配能够更快一些.这样的类使用的通用策略是,预先分配用于创建新对象的内存,需要时在预先分配的内存中构造每个新对象. 另外一些类希望按最小尺寸为自己的数据成员分配需要的内存.例如,