Sunday, November 9, 2008

LaTeX的画图宏包/工具太多了……

PSTricks, pgf/tikz, metapost, qtree, asymptote, graphviz .......
可视化的有 dia, kivio,inkscape........

虽说选择多种多样是好的,但是每一种都差别很远,也没有哪种是绝对最好的。
用起来有点痛苦,毕竟只是偶尔用一下,如果要精通就太浪费时间了,但是不精通又很快忘记。

这里大概记录一下吧。
PSTricks: 没用过,不表。据说不是很通用
pgf/tikz: 功能很强大,可以直接嵌入在LaTeX里使用,可以画几乎任何的图,另外有专门画树的功能,还能画自动机。2.0的manual竟然有560页。
metapost: 最nb的东西,可惜觉得语法有点脑残,不想去学…… 也是可以画很多东西,得到的是mps文件可以直接includegraphics
graphviz:用一种叫DOT的语言,画图论的图、自动机、目录树、关系图、ER图最方便了,但是因为是自动计算坐标位置,所以有时可能不能完全得到自己想要的位置。
支持输出为多种格式,甚至能直接输出为tex源文件(pgf/tikz语法)
asymptote:据说很nb的工具,可以支持3d效果,但是还没用过。

dia:类似visio,当然没visio那么强大,不过已经能凑合着使用了。
kivio:同上,功能似乎还弱点。
inkscape:画矢量图的利器,以前用过一次,但是觉得java运行起来有点慢就没用了。

Wednesday, November 5, 2008

[zz]林书记事件和谐版

作为受几十年教育的平凡人,作为一个有理想,有道德的大龄青年,我认为大家冤枉了我们的林书记,通过仔细研究视频,我认为事情的经过是这样的:林书记工作非常忙,日理万机,下班后根本没时间回家,还要利用吃饭的时间和一女人讨论建设祖国的事情,讨论到关键时,甚至于上厕所的时间也没有,林书记一边憋尿,一边对加强祖国的建设发表真知灼见。说时迟,那时快。正好有一11岁小学女生经过,林书记出于对祖国花朵的关心,详细地询问了小女孩的生产,生活及学习情况,并对小女孩的成长提出了宝贵意见。林书记终于憋不住尿了,为了不随地小便,污染环境,德高望重的林书记起身前往厕所。小女孩深深地为林书记的伟大人格魅力所吸引,于是也跟着林书记往厕所走,边走边汇报自己在过去的一年所取得的伟大成绩。到了厕所门口,学过中医的林书记不顾自己已经被尿憋得很难受,伸出双手放在小女孩脖子动脉处进行了一些把脉,通过诊断,林书记发现小女孩身体发育比较慢,于是林书记邀请小女孩进厕所,以便他注射一些蛋白质到小女孩体内,以促进小女孩的生长发育。小女孩被感动的哭了,坚决不接受上了年纪的林书记的恩惠,于是就跑开了.....  
这样的好书记,真是干部的榜样!

zz[电脑教程]令人目瞪口呆的四位世界级电脑大师

zz verycd.com 前面几段可以跳过去
g950g950 (楼主)  2007/8/9 顶楼 举报
「任何先进的科技一定是一种难以察觉的神奇力量。」

巫师一生之中一定都会有着一把拥有力量并潜藏危险的 "魔术法杖" 。巫婆会有属於他自己
啄养的 "神奇妖精" ,可以利用它们来制造大灾难。当然神秘术士会有他自己利用树木和铁
罐制作出来的 "泥人" 来听从他的命令。而我们也有我们的 "个人电脑"。

一台拥有自己生命且拥有强大创造力的 "神秘仪器" 。通常电脑会对滑鼠的移动有反应,或
是会利用任务执行结果来回应你念过的 "咒语" ,而且可以不利用 "超自然的力量" 来创造
你无法想像的惊人结果。虽然电脑可以执行我们给的命令,但是却难以平伏我们对 "电脑神
奇力量" 的好奇。

但时常发生的是电脑耍脾气、反抗你,甚至把你原本整齐的数字表搞得一团乱,你小心的输
入你的词句以及你漂亮的艺术创作,却被搞砸了。当这些事情发生时,我们确信我们是被电
脑耍了,电脑并没有在我们的控制之下,电脑正在尝试把我们原本有条不紊的东西搞得一团
乱。

无论个人电脑是我们忠实的 "仆人" 还是 "小恶魔" ,我们都可以知道一件事,就是这个沈
默的 "潘朵拉铁盒子" 里有着无数不为人知的秘密。打开它封印的 "潘朵拉铁盒" ,你会发
现到一堆毫无表情的元件,而我们只有少数的线索可以知道哪些是什麽。电脑内部大部分都
是长的像 "狮身女怪" 的微晶片,而且只提供一些奇怪的 "咒语编码" 在它那无法看透的表
面。其中还有 "电子回路迷宫" 、像是漂亮却毫无意义的 "象形文字" 。还有一些是非常重
要的元件,例如长得像 "法老王墓碑" 上印有危险、禁止拆解的硬盘和电源供应器。

一种值得深思熟虑并怡然自得的目标,我们可以深切地去思考你每天面对好几个小时的这个
 "潘朵拉的铁盒" ,知道他内部是如何运作来达成你的需求。如果这样可以解决你的问题或
让你发现新问题,那它就达到他的功用了。
=======================================================================
1.公元1998年在我�高二那年,那还是我用Windows 98的时候有次我系统崩溃了,

因为我那时还是个电脑白痴,我同学帮我介绍了一个电脑高手来帮我修电脑。

他看了一下电脑,问我有没有Windows 98的光盘?,我说没有。

他想了一下,叫我把家里电话拿给他,我心里想说修电脑要电话干什么?

但人家是电脑高手,我也不好意思说什么,就把电话拔下来给他了。

他把电话线空着的一头接在主机版的一个插孔内,然后进入了DOS,

然后就开始在电话上不停的按着键,他按键的速度非常快,但是他都只按电话的0,1两个键


我搞不懂这有什么用?但也不敢问,看了半个多小时,他还是不停的按这两个键,

我渐渐的有些困,我问他这东西要搞多久?他说还要几个小时,我给他倒了杯咖啡,

就一个人去隔壁书房睡觉了。

醒来的时候,一看已经过了5个多小时,我起身到隔壁,看见他正在Windows 98里面调试,

过了一会儿,他说,你试试,我坐上椅子用了一下,真的好了,我当时也不懂电脑,

谢过人家就走了。

后来我慢慢对电脑有了一些概念才了解到,

原来当时那位电脑高手是使用机器语言编了一个Windows 98系统

从我给他的电话直接输入到硬盘,

我后来问我同学那位高手的下落,我同学说前几年去了美国之后,杳无音讯....
========================================================================
2.公元2000年在我�大一那年,那还是我用Windows Me的时候有次我系统崩溃了,

因为我那时还是个电脑入门,我朋友帮我介绍了一个电脑高手来帮我修电脑。
  
他看了一下电脑,问我有没有Windows Me的安装盘?我说没有。
  
他想了一下,叫我把一张空的DVD刻录盘和一根奈米细针拿给他,

我心里想说修电脑要刻录盘和奈米细针干什么?但人家是电脑高手,我也不好意思说什么,

於是我就把DVD刻录盘拿一张来给他了。
  
他把奈米细针头对着刻录盘戳,他戳的速度非常快,但是只戳深或浅,

我搞不懂这有什么用?但也不敢问,看了半个多小时,他还是不停的戳着DVD刻录盘,

我渐渐的有些困,我问他这东西要搞多久?他说要几个小时,我给他倒了杯葡萄酒,

就一个人去隔壁客厅沙发上睡觉了。
  
醒来的时候,一看已经过了6个多小时,我起身到隔壁,看见他正在Windows Me里面调试,

还装上了Office、Photoshop、迅雷、魔兽世界等软件……过了一会儿他说,

你试试,我坐上椅子用了一下,真的好了,我当时也不懂电脑,谢过人家就走了。
  
后来我慢慢对电脑有了一些基础,终于明白了,

原来当时那位电脑高手是用奈米细针头刻了一个单面双层的DVD,在里面刻上了Windows Me
、Office、

Photoshop、魔兽世界的安装程序,我后来问我朋友那位高手的下落,

我朋友说前几年去了法国之后,杳无音讯....
========================================================================
3.公元2001年在我�大二那年,那还是我用Windows XP的时候有次我不小心把D盘格式化了


因为我那时还是个电脑新手,我亲戚帮我介绍了一个电脑高手来帮我的电脑D盘恢复数据。
  
他看了一下电脑,问我有没有Windows XP备份过的Ghost?我说没有。
  
他想了一下,叫我把一块奈米强力磁铁棒拿给他,还问我D盘里有什么东西?

我心里想说修电脑要奈米强力磁铁棒干什么?

但人家是电脑高手,我也不好意思说什么,就把奈米强力磁铁棒给他了,

还告诉他我的D盘里面全是火影忍者的动画。
  
他把整颗硬盘拆了,用奈米强力磁铁棒在盘子上面画圈圈,他画圈圈的速度非常快,

但是他的奈米强力磁铁棒好像有碰到又好像没有碰到,我搞不懂这有什么用?但也不敢问,

看了半个多小时,他还是不停的在硬盘上画着圈,我渐渐的有些困,

我问他这东西要搞多久?他说要几个小时,我给他倒了杯�格�威士忌,

就一个人去隔壁房间睡觉了。
  
醒来的时候,一看已经过了7个多小时,我起身到隔壁,看见他正在D盘里面调试,

里面全是火影忍者的动画片,过了一会儿,他说,

你试试,我坐上椅子用了一下,真的好了,我当时也不懂电脑,谢过人家就走了。
  
后来我慢慢对电脑有了一些体会,终于体悟到,

原来当时那位电脑高手是用奈米强力磁铁棒直接在硬盘上写数据,

他凭着惊人的记忆力将他曾经看过的火影忍者动画片数据都写入了硬盘,我后来问我亲戚那
位电脑高手的下落,

我亲戚说他前几年去了英国之后,杳无音讯....
========================================================================
4.就在最近,我刚装上的Vista突然自己崩溃了

就在最近,我刚装上的Vista突然自己崩溃了,虽然我会写很多程序,但是Vista崩溃了我也


有办法,我朋友给我介绍了一个高手来帮我修电脑。

他看了一下电脑,问我有没有Vista的安装盘,我说没有。

他想了一下,叫我拿一根没用的网线和一把剪刀,我想修电脑要网线和剪刀干什么,但人家

是高手,我也不好说什么,就拿了一根没用的网线和一把剪刀给他。

他把网线一头戳到网卡上,剪断另外一头,然后就在哪里不停的拨弄那八根线。他拨弄的速

度非常快,但是拨弄得线总是不一样,我搞不懂这有什么用,但也不敢问,看了半个多小

时,他还是不停的拨弄着网线。我渐渐的有些困,我问他这东西要搞多久,他说要几个小

时,我给他倒了杯茶,就一个人去隔壁睡觉了。

醒来的时候,一看已经过了4个多小时,我起身到隔壁,看见他正在Vista里面调试,还装给

装好了Visual Studio 2005 Team Suit、Ms SqlServer 2005 Enterprise Edition、BT,还


下载边看American片……过了一会儿,他惊觉我站在后面,不好意思地对我说,你试试。我

坐上椅子用了一下,真的好了,我当时太震惊了,整个人傻在哪里,谢过人家就走了。

后来我读到了前面那两篇文章,终于醒悟,原来当时那位高手是用网线模拟网络启动,下载

了整个Vista,还有Visual Studio 2005 Team Suit、Ms SqlServer 2005 Enterprise
Edition、

BT,以及他正在看的American片,我后来问我朋友那位高手的下落,我朋友说前几年去了

美国之后,杳无音讯了十几年,期间也回过中国两次,最近这一次回来说,干这个太没意思

了,回来收拾收拾准备去阿尔法半人马座,他说他也听不懂那高手说什么,只是最近传闻微

软的Vista全世界都无法激活,估计跟他的离去有关,当然了,这几天也是杳无音讯....

Monday, November 3, 2008

[zz]C/C++中的序列点

From newsmth:

0. 什么是副作用(side effects)

C99定义如下
Accessing a volatile object, modifying an object, modifying a file, or
calling a function that does any of those operations are all side effects,
which are changes in the state of the execution environment.

C++2003定义如下
Accessing an object designated by a volatile lvalue, modifying an object,
calling a library I/O function, or calling a function that does any of
those operations are all side effects, which are changes in the state of
the execution environment.

可以看出C99和C++2003对副作用的定义基本类似,一个程序可以看作一个状态机,在
任意一个时刻程序的状态包含了它的所有对象内容以及它的所有文件内容(标准输入
输出也是文件),副作用会导致状态的跳转

一个变量一旦被声明为volatile-qualified类型,则表示该变量的值可能会被程序之
外的事件改变,每次读取出来的值只在读取那一刻有效,之后如果再用到该变量的值
必须重新读取,不能沿用上一次的值,因此读取volatile-qualified类型的变量也被
认为是有副作用,而不仅仅是改写

注,一般不认为程序的状态包含了CPU寄存器的内容,除非该寄存器代表了一个变量,
例如
void foo() {
  register int i = 0;  // 变量i被直接放入寄存器中,本文中被称为寄存器变量
                       // 注,register只是一个建议,不一定确实放入寄存器中
                       // 而且没有register关键字的auto变量也可能放入寄存器
                       // 这里只是用来示例,假设i确实放入了寄存器中
  i = 1;  // 寄存器内容改变,对应了程序状态的改变,该语句有副作用
  i + 1;  // 编译时该语句一般有警告:"warning: expression has no effect"
          // CPU如果执行这个语句,也肯定会改变某个寄存器的值,但是程序状态
          // 并未改变,除了代表i的寄存器,程序状态不包含其他寄存器的内容,
          // 因此该语句没有任何副作用
}
特别的,C99和C++2003都指出,no effect的expression允许不被执行
An actual implementation need not evaluate part of an expression if it
can deduce that its value is not used and that no needed side effects
are produced (including any caused by calling a function or accessing
a volatile object).


1. 什么是序列点(sequence points)

C99和C++2003对序列点的定义相同
At certain specified points in the execution sequence called sequence
points, all side effects of previous evaluations shall be complete and
no side effects of subsequent evaluations shall have taken place.

中文表述为,序列点是一些被特别规定的位置,要求在该位置前的evaluations所
包含的一切副作用在此处均已完成,而在该位置之后的evaluations所包含的任何
副作用都还没有开始

例如C/C++都规定完整表达式(full-expression)后有一个序列点
extern int i, j;
i = 0;
j = i;
上面的代码中i = 0以及j = i都是一个完整表达式,;说明了表达式的结束,因此
在;处有一个序列点,按照序列点的定义,要求在i = 0之后j = i之前的那个序列
点上对i = 0的求值以及副作用全部结束(0被写入i中),而j = i的任何副作用都
还没有开始。由于j = i的副作用是把i的值赋给j,而i = 0的副作用是把i赋值为
0,如果i = 0的副作用发生在j = i之后,就会导致赋值后j的值是i的旧值,这显
然是不对的

由序列点以及副作用的定义很容易看出,在一个序列点上,所有可能影响程序状态
的动作均已完成,那这样能否推断出在一个序列点上一个程序的状态应该是确定的
呢?!答案是不一定,这取决于我们代码的写法。但是,如果在一个序列点上程序
的状态不能被确定,那么标准规定这样的程序是undefined behavior,稍后会解释
这个问题


2. 表达式求值(evaluation of expressions)与副作用发生的相互顺序

C99和C++2003都规定
Except where noted, the order of evaluation of operands of individual
operators and subexpressions of individual expressions, and the order
in which side effects take place, is unspecified.

也就是说,C/C++都指出一般情况下在表达式求值过程中的操作数求值顺序以及副
作用发生顺序是未说明的(unspecified)。为什么C/C++不详细定义这些顺序呢?
原因是因为C/C++都是极端追求效率的语言,不规定这些顺序,是为了允许编译器
有更大的优化余地,例如
extern int *p;
extern int i;
*p = i++;  // (1)
根据前述规定,在表达式(1)中到底是*p先被求值还是i++先被求值是由编译器决定
的;两次副作用(对*p赋值以及i++)发生的顺序是由编译器决定的;甚至连子表
达式i++的求值(就是初始时i的值)以及副作用(将i增加1)都不需要同步发生,
编译器可以先用初始时i的值(即子表达式i++的值)对*p赋值,然后再将i增加1,
这样就把子表达式i++的整个计算过程分成了两个不相邻的步骤。而且通常编译器
都是这么实现的,原因在于i++的求值过程同*p = i++是有区别的,对于单独的表
达式i++,执行顺序一般是(假设不考虑inc指令):先将i加载到某个寄存器A(如
果i是寄存器变量则此步骤可以跳过)、将寄存器A的值加1、将寄存器A的新值写回
i的地址;对于*p = i++,如果要先完整的计算子表达式i++,由于i++表达式的值
是i的旧值,因此还需要一个额外的寄存器B以及一条额外的指令来辅助*p = i++的
执行,但是如果我们先将加载到A的值写回到*p,然后再执行对i增加1的指令,则
只需要一个寄存器即可,这种做法在很多平台都有重要意义,因为寄存器的数目往
往是有限的,特别是假如有人写出如下的语句
extern int i, j, k, x;
x = (i++) + (j++) + (k++);
编译器可以先计算(i++) + (j++) + (k++)的值,然后再对i、j、k各自加1,最后
将i、j、k、x写回内存,这比每次完整的执行完++语义效率要高


3. 序列点对副作用的限制

C99和C++2003都有类似的如下规定
Between the previous and next sequence point a scalar object shall
have its stored value modified at most once by the evaluation of an
expression. Furthermore, the prior value shall be accessed only to
determine the value to be stored. The requirements of this paragraph
shall be met for each allowable ordering of the subexpressions of a
full expression; otherwise the behavior is undefined.

也就是说,在相邻的两个序列点之间,一个对象只允许被修改一次,而且如果一个
对象被修改则在这两个序列点之间对该变量的读取的唯一目的只能是为了确定该对
象的新值(例如i++,需要先读取i的值以确定i的新值是旧值+1)。特别的,标准
要求任意可能的执行顺序都必须满足该条件,否则代码将是undefined behavior

之所以序列点会对副作用有如此的限制,就是因为C/C++标准没有规定子表达式求
值以及副作用发生之间的顺序,例如
extern int i, a[];
extern int foo(int, int);
i = ++i + 1;  // 该表达式对i所做的两次修改都需要写回对象,i的最终值取决
              // 于到底哪次写回最后发生,如果赋值动作最后写回,则i的值
              // 是i的旧值加2,如果++i动作最后写回,则i的值是旧值加1,
              // 因此该表达式的行为是undefined
a[i++] = i;  // 如果=左边的表达式先求值并且i++的副作用被完成,则右边的
             // 值是i的旧值加1,如果i++的副作用最后完成,则右边的值是i
             // 的旧值,这也导致了不确定的结果,因此该表达式的行为将是
             // undefined
foo(foo(0, i++), i++);  // 对于函数调用而言,标准没有规定函数参数的求值
                        // 顺序,但是标准规定所有参数求值完毕进入函数体
                        // 执行之前有一个序列点,因此这个表达式有两种执
                        // 行方式,一种是先求值外层foo调用的i++然后求值
                        // foo(0, i++),然后进入到foo(0, i++)执行,这之
                        // 前有个序列点,这种执行方式还是在两个相邻序列
                        // 点之间修改了i两次,undefined
                        // 另一种执行方式是先求值foo(0, i++),由于这里
                        // 有一个序列点,随后的第二个i++求值是在新序列
                        // 点之后,因此不算是两个相邻的序列点之间修改i
                        // 两次
                        // 但是,前面已经指出标准规定任意可能的执行路径
                        // 都必须满足条件才是定义好的行为,这种代码仍然
                        // 是undefined

前面我提到在一个序列点上程序的状态不一定是确定的,原因就在于相邻的两个序
列点之间可能会发生多个副作用,这些副作用的发生顺序是未指定的,如果多于一
个的副作用用于修改同一个对象,例如示例代码i = ++i + 1;,则程序的结果是依
赖于副作用发生顺序的;另外,如果某个表达式既修改了某个对象又需要读取该对
象的值,且读取对象的值并不用于确定对象新值,则读取和修改两个动作的先后顺
序也会导致程序的状态不能唯一确定
所幸的是,"在相邻的两个序列点之间,一个对象只允许被修改一次,而且如果一
个对象被修改则在这两个序列点之间只能为了确定该对象的新值而读一次"这一强
制规定保证了符合要求的程序在任何一个序列点位置上其状态都可以确定下来

注,由于对于UDT类型存在operator重载,函数语义会提供新的序列点,因此某些
对于built-in类型是undefined behavior的表达式对于UDT确可能是良好定义的,
例如
i = i++;  // 如果i是built-in类型对象,则该表达式在两个相邻的序列点之间对
          // i修改了两次,undefined
          // 如果i是UDT类型该表达式也许是i.operator=(i.operator++(int)),
          // 函数参数求值完毕后会有一个序列点,因此该表达式并没有在两个
          // 相邻的序列点之间修改i两次,OK

由此可见,常见的问题如printf("%d, %d", i++, i++)这种写法是错误的,这类问
题作为笔试题或者面试题是没有任何意义的
类似的问题同样发生在cout << i++ << i++这种写法上,如果overload resolution
选择成员函数operator<<,则等价于(cout.operator<<(i++)).operator<<(i++),
否则等价于operator<<(operator<<(cout, i++), i++),如果i是built-in类型对
象,这种写法跟foo(foo(0, i++), i++)的问题一致,都是未定义行为,因为存在
某条执行路径使得i会在两个相邻的序列点之间被修改两次;如果i是UDT则该写法
是良好定义的,跟i = i++一样,但是这种写法也是不推荐的,因为标准对于函数
参数的求值顺序是unspecified,因此哪个i++先计算是不能预计的,这仍旧会带来
移植性的问题,这种写法应该避免


4. 编译器的跨序列点优化

根据前述讨论可知,在同一个表达式内对于同一个变量i,允许的行为是
A. 不读取,改写一次,例如
     i = 0;
B. 读取一次或者多次,改写一次,但所有读取仅仅用于决定改写后的新值,例如
     i = i + 1;  // 读取一次,改写一次
     i = i & (i - 1);  // 读取两次,改写一次,感谢puke给出的例子
C. 不改写,读取一次或者多次,例如
     j = i & (i - 1);

对于情况B和C,编译器是有一定的优化权利的,它可以只读取一次变量的值然后
直接使用该值多次

但是,当该变量是volatile-qualified类型时编译器允许的行为究竟如何目前还
没有找到明确的答案,ctrlz认为如果在两个相邻序列点之间读取同一个volatile-
qualified类型对象多次仍旧是undefined behavior,原因在于该读取动作有副作
用且该副作用等价于修改该对象,RoachCock的意见是两个相邻的序列点之间读取
同一个volatile-qualified类型应该是合法的,但是不能被优化成只读一次。一
段在嵌入式开发中很常见的代码示例如下
extern volatile int i;
if (i != i) {  // 探测很短的时间内i是否发生了变化
  // ...
}
如果i != i被优化为只读一次,则结果恒为false,故RoachCock认为编译器不能
够对volatile-qualified类型的变量做出只读一次的优化。ctrlz则认为这段代码
本身是不正确的,应该改写成
int j = i;
if (j != i) {  // 将对volatile-qualified类型变量的多次读取用序列点隔开
  // ...
}

虽然尚不能确定volatile-qualified类型的变量在相邻两个序列点之间读取多次
行为是否合法以及将如何优化(不管怎么样,对于volatile-qualified类型这种
代码应该尽量避免),但是可以肯定的是,对于volatile-qualified类型的变量
在跨序列点之后必须要重新读取,volatile就是用来阻止编译器做出跨序列点的
过激优化的,而对于non-volatile-qualified类型的跨序列点多次读取则可能被
优化成只读一次(直到某个语句或者函数对该变量发生了修改,在此之前编译器
可以假定non-volatile-qualified类型的变量是不会变化的,因为目前的C/C++
抽象机器模型是单线程的),例如
bool flag = true;
void foo() {
  while (flag) {  // (2)
    // ...
  }
}
如果编译器探测到foo()没有任何语句(包括foo()调用过的函数)对flag有过修
改,则也许会把(2)优化成只在进入foo()的时候读一次flag的值而不是每次循环
都读一次,这种跨序列点的优化很有可能导致死循环。但是这种代码在多线程编
程中很常见,虽然foo()没有修改过flag,也许在另一个线程的某个函数调用中
会修改flag以终止循环,为了避免这种跨序列点优化带来到错误,应该把flag声
明为volatile bool,C++2003对volatile的说明如下
[Note: volatile is a hint to the implementation to avoid aggressive
optimization involving the object because the value of the object
might be changed by means undetectable by an implementation. See 1.9
for detailed semantics. In general, the semantics of volatile are
intended to be the same in C++ as they are in C. ]


5. C99定义的序列点列表

― The call to a function, after the arguments have been evaluated.
― The end of the first operand of the following operators:
     logical AND && ;
     logical OR || ;
     conditional ? ;
     comma , .
― The end of a full declarator:
     declarators;
― The end of a full expression:
     an initializer;
     the expression in an expression statement;
     the controlling expression of a selection statement (if or switch);
     the controlling expression of a while or do statement;
     each of the expressions of a for statement;
     the expression in a return statement.
― Immediately before a library function returns.
― After the actions associated with each formatted input/output function
   conversion specifier.
― Immediately before and immediately after each call to a comparison
   function, and also between any call to a comparison function and any
   movement of the objects passed as arguments to that call.


6. C++2003定义的序列点列表

所有C99定义的序列点同样是C++2003所定义的序列点
此外,C99只是规定库函数返回之后有一个序列点,并没有规定普通函数返回之后
有一个序列点,而C++2003则特别指出,进入函数(function-entry)和退出函数
(function-exit)各有一个序列点,即拷贝一个函数的返回值之后同样存在一个
序列点

需要特别说明的是,由于operator||、operator&&以及operator,可以重载,当它
们使用函数语义的时候并不提供built-in operators所规定的那几个序列点,而
仅仅只是在函数的所有参数求值后有一个序列点,此外函数语义也不支持||、&&
的短路语义,这些变化很有可能会导致难以发觉的错误,因此一般不建议重载这
几个运算符


7. C++2003中两处关于lvalue的修改对序列点的影响

在C语言中,assignment operators的结果是non-lvalue,C++2003则将assignment
operators的结果改成了lvalue,目前尚不清楚这一改动对于built-in类型有何意
义,但是它却导致了很多在合法的C代码在目前的C++中是undefined behavior,例

extern int i;
extern int j;
i = j = 1;
由于(j = 1)的结果是lvalue,该结果作为给i赋值的右操作数,需要一个lvalue-
to-rvalue conversion,这个conversion代表了一个读取语义,因此i = j = 1就
是先将1赋值给j,然后读取j的值赋值给i,这个行为是undefined,因为标准规定
两个相邻序列点之间的读取只能用于决定修改对象的新值,而不能发生在修改之后
再读取
由于C++2003规定assignment operators的结果是lvalue,因此下列在C99中非法的
代码在C++2003中却是可以通过编译的
extern int i;
(i += 1) += 2;
显然按照C++2003的规定这个代码的行为是undefined,它在两个相邻的序列点之间
修改了i两次

类似的问题同样发生在built-in类型的前缀++/--operators上,C++2003将前缀++/--
的结果从rvalue修改为lvalue,这甚至导致了下列代码也是undefined behavior
extern int i;
extern int j;
i = ++j;
同样是因为lvalue作为assignment operator的右操作数需要一个左值转换,该转
换导致了一个读取动作且这个读取动作发生在修改对象之后

C++的这一改动显然是考虑不周的,导致了很多C语言的习惯写法都成了undefined
behavior,因此Andrew Koenig在1999年的时候就向C++标准委员会提交了一个建
议要求为assignment operators增加新的序列点,但是到目前为止C++标准委员会
都还没有就该问题达成一致意见,我将Andrew Koenig的提议附后,如果哪位有时
间有兴趣,可以看看,不过不看也不会有任何损失 :-)


222. Sequence points and lvalue-returning operators
Section: 5  expr     Status: drafting     Submitter: Andrew Koenig     Date: 20 Dec 1999

I believe that the committee has neglected to take into account one of the differences between C and C++ when defining sequence points. As an example, consider

    (a += b) += c;

where a, b, and c all have type int. I believe that this expression has undefined behavior, even though it is well-formed. It is not well-formed in C, because += returns an rvalue there. The reason for the undefined behavior is that it modifies the value of `a' twice between sequence points.

Expressions such as this one are sometimes genuinely useful. Of course, we could write this particular example as

    a += b; a += c;

but what about

    void scale(double* p, int n, double x, double y) {
        for (int i = 0; i < n; ++i) {
            (p[i] *= x) += y;
        }
    }

All of the potential rewrites involve multiply-evaluating p[i] or unobvious circumlocations like creating references to the array element.

One way to deal with this issue would be to include built-in operators in the rule that puts a sequence point between evaluating a function's arguments and evaluating the function itself. However, that might be overkill: I see no reason to require that in

    x[i++] = y;

the contents of `i' must be incremented before the assignment.

A less stringent alternative might be to say that when a built-in operator yields an lvalue, the implementation shall not subsequently change the value of that object as a consequence of that operator.

I find it hard to imagine an implementation that does not do this already. Am I wrong? Is there any implementation out there that does not `do the right thing' already for (a += b) += c?

5.17  expr.ass paragraph 1 says,

The result of the assignment operation is the value stored in the left operand after the assignment has taken place; the result is an lvalue.
What is the normative effect of the words "after the assignment has taken place"? I think that phrase ought to mean that in addition to whatever constraints the rules about sequence points might impose on the implementation, assignment operators on built-in types have the additional constraint that they must store the left-hand side's new value before returning a reference to that object as their result.

One could argue that as the C++ standard currently stands, the effect of x = y = 0; is undefined. The reason is that it both fetches and stores the value of y, and does not fetch the value of y in order to compute its new value.

I'm suggesting that the phrase "after the assignment has taken place" should be read as constraining the implementation to set y to 0 before yielding the value of y as the result of the subexpression y = 0.

Note that this suggestion is different from asking that there be a sequence point after evaluation of an assignment. In particular, I am not suggesting that an order constraint be imposed on any side effects other than the assignment itself.
Francis Glassborow:

My understanding is that for a single variable:

Multiple read accesses without a write are OK
A single read access followed by a single write (of a value dependant on the read, so that the read MUST happen first) is OK
A write followed by an actual read is undefined behaviour
Multiple writes have undefined behaviour
It is the 3) that is often ignored because in practice the compiler hardly ever codes for the read because it already has that value but in complicated evaluations with a shortage of registers, that is not always the case. Without getting too close to the hardware, I think we both know that a read too close to a write can be problematical on some hardware.

So, in x = y = 0;, the implementation must NOT fetch a value from y, instead it has to "know" what that value will be (easy because it has just computed that in order to know what it must, at some time, store in y). From this I deduce that computing the lvalue (to know where to store) and the rvalue to know what is stored are two entirely independent actions that can occur in any order commensurate with the overall requirements that both operands for an operator be evaluated before the operator is.

Erwin Unruh:

C distinguishes between the resulting value of an assignment and putting the value in store. So in C a compiler might implement the statement x=y=0; either as x=0;y=0; or as y=0;x=0; In C the statement (x += 5) += 7; is not allowed because the first += yields an rvalue which is not allowed as left operand to +=. So in C an assignment is not a sequence of write/read because the result is not really "read".

In C++ we decided to make the result of assignment an lvalue. In this case we do not have the option to specify the "value" of the result. That is just the variable itself (or its address in a different view). So in C++, strictly speaking, the statement x=y=0; must be implemented as y=0;x=y; which makes a big difference if y is declared volatile.

Furthermore, I think undefined behaviour should not be the result of a single mentioning of a variable within an expression. So the statement (x +=5) += 7; should NOT have undefined behaviour.

In my view the semantics could be:

if the result of an assignment is used as an rvalue, its value is that of the variable after assignment. The actual store takes place before the next sequence point, but may be before the value is used. This is consistent with C usage.
if the result of an assignment is used as an lvalue to store another value, then the new value will be stored in the variable before the next sequence point. It is unspecified whether the first assigned value is stored intermediately.
if the result of an assignment is used as an lvalue to take an address, that address is given (it doesn't change). The actual store of the new value takes place before the next sequence point.
Jerry Schwarz:

My recollection is different from Erwin's. I am confident that the intention when we decided to make assignments lvalues was not to change the semantics of evaluation of assignments. The semantics was supposed to remain the same as C's.

Ervin seems to assume that because assignments are lvalues, an assignment's value must be determined by a read of the location. But that was definitely not our intention. As he notes this has a significant impact on the semantics of assignment to a volatile variable. If Erwin's interpretation were correct we would have no way to write a volatile variable without also reading it.

Lawrence Crowl:

For x=y=0, lvalue semantics implies an lvalue to rvalue conversion on the result of y=0, which in turn implies a read. If y is volatile, lvalue semantics implies both a read and a write on y.

The standard apparently doesn't state whether there is a value dependence of the lvalue result on the completion of the assignment. Such a statement in the standard would solve the non-volatile C compatibility issue, and would be consistent with a user-implemented operator=.

Another possible approach is to state that primitive assignment operators have two results, an lvalue and a corresponding "after-store" rvalue. The rvalue result would be used when an rvalue is required, while the lvalue result would be used when an lvalue is required. However, this semantics is unsupportable for user-defined assignment operators, or at least inconsistent with all implementations that I know of. I would not enjoy trying to write such two-faced semantics.

Erwin Unruh:

The intent was for assignments to behave the same as in C. Unfortunately the change of the result to lvalue did not keep that. An "lvalue of type int" has no "int" value! So there is a difference between intent and the standard's wording.

So we have one of several choices:

live with the incompatibility (and the problems it has for volatile variables)
make the result of assignment an rvalue (only builtin-assignment, maybe only for builtin types), which makes some presently valid programs invalid
introduce "two-face semantics" for builtin assignments, and clarify the sequence problematics
make a special rule for assignment to a volatile lvalue of builtin type
I think the last one has the least impact on existing programs, but it is an ugly solution.

Andrew Koenig:

Whatever we may have intended, I do not think that there is any clean way of making

    volatile int v;
    int i;

    i = v = 42;

have the same semantics in C++ as it does in C. Like it or not, the subexpression v = 42 has the type ``reference to volatile int,'' so if this statement has any meaning at all, the meaning must be to store 42 in v and then fetch the value of v to assign it to i.

Indeed, if v is volatile, I cannot imagine a conscientious programmer writing a statement such as this one. Instead, I would expect to see

    v = 42;
    i = v;

if the intent is to store 42 in v and then fetch the (possibly changed) value of v, or
    v = 42;
    i = 42;

if the intent is to store 42 in both v and i.

What I do want is to ensure that expressions such as ``i = v = 42'' have well-defined semantics, as well as expressions such as (i = v) = 42 or, more realistically, (i += v) += 42 .

I wonder if the following resolution is sufficient:

Append to 5.17  expr.ass paragraph 1:

There is a sequence point between assigning the new value to the left operand and yielding the result of the assignment expression.
I believe that this proposal achieves my desired effect of not constraining when j is incremented in x[j++] = y, because I don't think there is a constraint on the relative order of incrementing j and executing the assignment. However, I do think it allows expressions such as (i += v) += 42, although with different semantics from C if v is volatile.

Notes on 10/01 meeting:

There was agreement that adding a sequence point is probably the right solution.

Notes from the 4/02 meeting:

The working group reaffirmed the sequence-point solution, but we will look for any counter-examples where efficiency would be harmed.

For drafting, we note that ++x is defined in 5.3.2  expr.pre.incr as equivalent to x+=1 and is therefore affected by this change. x++ is not affected. Also, we should update any list of all sequence points.

Notes from October 2004 meeting:

Discussion centered around whether a sequence point "between assigning the new value to the left operand and yielding the result of the expression" would require completion of all side effects of the operand expressions before the value of the assignment expression was used in another expression. The consensus opinion was that it would, that this is the definition of a sequence point. Jason Merrill pointed out that adding a sequence point after the assignment is essentially the same as rewriting

    b += a

as

    b += a, b

Clark Nelson expressed a desire for something like a "weak" sequence point that would force the assignment to occur but that would leave the side effects of the operands unconstrained. In support of this position, he cited the following expression:

    j = (i = j++)

With the proposed addition of a full sequence point after the assignment to i, the net effect is no change to j. However, both g++ and MSVC++ behave differently: if the previous value of j is 5, the value of the expression is 5 but j gets the value 6.

Clark Nelson will investigate alternative approaches and report back to the working group.

一道很tricky的题目

int a,b,c;
int *p;
a=100;
c=10;
p=&c;
b=a/*p;
printf("%d",b);

输出是啥?











































没有输出,编译不通过,因为/*是注释的开始……