学习笔记(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;
学习笔记(C++Primer)--易错点总结(Chapter2)的更多相关文章
- ref:学习笔记 UpdateXml() MYSQL显错注入
ref:https://www.cnblogs.com/MiWhite/p/6228491.html 学习笔记 UpdateXml() MYSQL显错注入 在学习之前,需要先了解 UpdateXml( ...
- 【整理】解决vue不相关组件之间的数据传递----vuex的学习笔记,解决报错this.$store.commit is not a function
解决vue不相关组件之间的数据传递----vuex的学习笔记,解决报错this.$store.commit is not a function https://www.cnblogs.com/jaso ...
- 学习笔记 UpdateXml() MYSQL显错注入
在学习之前,需要先了解 UpdateXml() . UPDATEXML (XML_document, XPath_string, new_value); 第一个参数:XML_document是Stri ...
- TensorFlow学习笔记(MNIST报错修正 适用Tensorflow1.3)
在Tensorflow实战Google框架下的深度学习这本书的MNIST的图像识别例子中,每次都要报错 错误如下: Only call `sparse_softmax_cross_entropy_ ...
- 解决vue不相关组件之间的数据传递----vuex的学习笔记,解决报错this.$store.commit is not a function
Vue的项目中,如果项目简单, 父子组件之间的数据传递可以使用 props 或者 $emit 等方式 进行传递 但是如果是大中型项目中,很多时候都需要在不相关的平行组件之间传递数据,并且很多数据需要 ...
- python学习笔记-import utils报错
今天遇到一个坑爹的问题,查找了半天原因,终于解决了,在此特地记录一下. 运行环境:Windows eclipse 我在eclipse中配置了python的运行环境,在eclipse中编写python代 ...
- Java学习笔记之Scanner报错java.util.NoSuchElementException
转载自:IT学习者-螃蟹 一个方法A使用了Scanner,在里面把它关闭了.然后又在方法B里调用方法A之后就不能再用Scanner了Scanner in = new Scanner(System.in ...
- 学习笔记71—Python 报错处理集
****************************************************** 如有谬误,请联系指正.转载请注明出处. 联系方式: e-mail: heyi9069@gm ...
- 关于java学习中的一些易错点(基础篇)
由JVM来负责Java程序在该系统中的运行,不同的操作系统需要安装不同的JVM,这样Java程序只需要跟JVM打交道,底层的操作由JVM去执行. JRE(Java Runtime Environmen ...
- ------------------java collection 集合学习 ----小白学习笔记,,有错,请指出谢谢
<!doctype html>java对象集合学习记录 figure:first-child { margin-top: -20px; } #write ol, #write ul { p ...
随机推荐
- WPF刷新界面之坎坷路
WPF刷新界面之坎坷路 项目需要一个硬件检测功能,需要用到界面刷新,刚开始想用个定时器,对检测过的硬设定时添加后刷新界面. 但是很遗憾,定时器并不能进行刷新.后台检测List数据里面已经添加了很多了很 ...
- Arduino 各种模块篇 粉尘传感器 dust sensor 空气质量检测
Testing a sensor from here. http://www.seeedstudio.com/wiki/Grove_-_Dust_Sensor It's a dust sensor. ...
- set 类型
set类型 map 容器是键-值对的集合,好比以人名为键的地址和电话号码. 相反地,set 容器只是单纯的键的集合.map 适用于字典.电话本.商品价目表等类似的模型.set 适用于黑名单.白名单等. ...
- 挖一下插件v1.5版本发布
Chrome图片下载插件,支持网页截屏 v.1.5更新说明: 1.增加下载图片按日期分类保存选项,便于管理,用户可根据需要开启/关闭此设置 2.增加网页图片采集快捷键: (1)采集页面图片(Ctrl+ ...
- The Event System
The Event System 在Qt中,事件是继承了虚拟类QEvent的对象,它代表了程序所发生的事情或者程序需要知道的一个外部活动的结果.事件可以被任意 QObject子类的实例接收和处理,是与 ...
- 作怪的Buffer
俗话说:人丑多作怪.在编程界里面也有很多作怪之物,其中首推buffer. 上一次聊到了tar.gz创建导出的问题,我本以为自己把相关的文件流操作都摸清楚了.没想到当我开心地去研究ip库替换方案和同事们 ...
- 【原】tinker dex文件格式的dump工具tinker-dex-dump
序言 Tinker是微信推出的热更新开源项目,同其它热更新方案相比具有补丁包小,支持类,so,资源文件的替换等优点.其中在类替换的方案里自主研发了DexDiff算法,使得补丁包变的更小.DexDiff ...
- Jquery源码分析与简单模拟实现
前言 最近学习了一下jQuery源码,顺便总结一下,版本:v2.0.3 主要是通过简单模拟实现jQuery的封装/调用.选择器.类级别扩展等.加深对js/Jquery的理解. 正文 先来说问题: 1. ...
- 浅谈Web前端浏览器兼容问题
对于兼容最近一直困扰我,以前写的代码只是针对高质量用户来使用 不考虑IE7,8 这样的浏览器 ,但是最近我开发的时候必须要兼容,大喊一声我曹,没有办法,自己来吧! 所谓的浏览器兼容性问题,是指因为不同 ...
- Kubernetes(k8s)容器运行时(CRI)
Kubernetes节点的底层由一个叫做"容器运行时"的软件进行支撑,它负责比如启停容器这样的事情.最广为人知的容器运行时当属Docker,但它不是唯一的.事实上,容器运行时这个领 ...