一个简单的程序引发了一块让人纠结的领域,也许强调编程规范的重要性也在这把。规范了就easy避免一些问题。

程序是这种

int Change(int& a)
{
a = 4;
return a;
} int main()
{
int a = 10;
cout << Change(a) << a;
}

In C-Free : the output : 4 4

In VS2008 : the output : 4 10

差别出来了,按我的理解,应该C-free输出的对,可是还是探究了下,下面是我别人给我的回复:

Simply put, there is no rule that guarantees that "4 4" is right. Same goes for "4 10".

As others have mentioned in the comments you have a case of undefined behaviour here. Even if this would be defined behaviour, code like this is difficult to understand. So I recommend to do

cout << Change(a);
cout << a;

or

int b = a;
cout << Change(a);
cout << b;

whatever you really want to do.

另外,找到了C++不确定行为的一片天,有兴趣的能够在这里继续探究非常多C++出现不确定行为的情况。

http://en.cppreference.com/w/cpp/language/eval_order

随机推荐

  1. UTL_RAW

    The UTL_RAW package provides SQL functions for manipulating RAW data types. 该包的功能其实可以用来加密: SELECT    ...

  2. cocos2d-x游戏开发系列教程-中国象棋05-开始游戏

    前情回顾 通过CCMainMenu的init函数,已经把所有的按钮,棋子都摆放完毕了,但是这个时候,棋子是不能走动的,只有在开始游戏之后才能移动棋子. 点击

  3. 基于visual Studio2013解决C语言竞赛题之0703乾坤大挪移

       题目

  4. (step6.1.5)hdu 1233(还是畅通工程——最小生成树)

    题目大意:输入一个整数n,表示有n个村庄,在接下来的n*(n-1)/2中,每行有3个整数beigin.end.weight,分别表示路的起始村庄,结束村庄和村庄之间的距离. 求索要修的路的最短距离 解 ...

  5. 数据绑定以及Container.DataItem几种方式与使用方法分析

    灵活的运用数据绑定操作        绑定到简单属性:<%#UserName%>        绑定到集合:<asp:ListBox id="ListBox1" ...

  6. 百度地图api基本用法

    首先 ,如果想调用百度地图api,你需要获取一个百度地图api的密钥. 申请密钥很简单,在百度地图api的首页就有相关链接,填写相关信息百度就会给你一个密钥了. 接下来,就是引入百度地图的api 关键 ...

  7. 如何查看VS中预设的路径变量

    类似"$(VCInstallDir)"之类的变量查询方法为:打开VS命令行提示窗口,输入 Set 命令. VS中“Tool” - “Visual Studio Command Pr ...

  8. Windows Services的1053错误的解决办法之一:修改注册表允许的响应时间

    Error: 'The service did not respond in a timely fashion' (ServicesPipeTimeout) when attempting when ...

  9. Windows Phone 8初学者开发的翻译终于过半

    从2013年7月19日开始,到2013年12月9日,一共花了143天时间完成了18篇Windows Phone 8初学者开发的翻译,还剩下17篇文章需要翻译,看到了完成的希望! I love Wind ...

  10. perl json模块

    JSON - JSON (JavaScript Object Notation) encoder/decoder 简介: use JSON; # imports encode_json, decode ...