转自:http://stackoverflow.com/questions/8848402/whats-the-difference-between-call-by-reference-and-copy-restore

Main code:

#include <stdio.h>

  int a;

  int main() {
a = 3;
f( &a, 4);
printf("%d\n", a);
return 0;
}

Call by Value:

f(int x, int &y){
// x will be 3 as passed argument
x += a;
// now a is added to x so x will be 6
// but now nothing is done with x anymore
a += 2*y;
// a is still 3 so the result is 11
}

Value is passed in and has no effect on the value of the variable passed in.

Call by Reference:

f(int x, int &y){
// x will be 3 as passed argument
x += a;
// now a is added to x so x will be 6
// but because & is used x is the same as a
// meaning if you change x it will change a
a += 2*y;
// a is now 6 so the result is 14
}

Reference is passed in. Effectively the variable in the function is the same as the one outside.

Call with Copy/Restore:

int a;
void unsafe(int x) {
x= 2; //a is still 1
a= 0; //a is now 0
}//function ends so the value of x is now stored in a -> value of a is now 2 int main() {
a= 1;
unsafe(a); //when this ends the value of a will be 2
printf("%d\n", a); //prints 2
}

Value is passed in and has no effect on the value of the variable passed in UNTIL the end of the function, at which point the FINAL value of the function variable is stored in the passed in variable.

The basic difference between call by reference and copy/restore then is that changes made to the function variable will not show up in the passed in variable until after the end of the function while call by reference changes will be seen immediately.

call by reference and copy/restore的更多相关文章

  1. JavaScript : Array assignment creates reference not copy

    JavaScript : Array assignment creates reference not copy 29 May 2015 Consider we have an array var a ...

  2. [C++] Copy Control (part 1)

    Copy, Assign, and Destroy When we define a class, we specify what happens when objects of the class ...

  3. std::copy使用方法

    推荐2个c++函数库,类定义的资料库: http://en.cppreference.com/w/cpp/algorithm/copy http://www.cplusplus.com/referen ...

  4. [翻译]Writing Custom DB Engines 编写定制的DB引擎

    Writing Custom DB Engines  编写定制的DB引擎   FastReport can build reports not only with data sourced from ...

  5. HEC-ResSim原文档

              HEC-ResSim Reservoir System Simulation             User's Manual       Version 3.1 May 201 ...

  6. 数据结构《19》----String容器的三种实现

    一.序言 一个简单的string 容器到底是如何实现的? 本文给出了 String 的三种从易到难的实现,涉及了 reference counting, copy on write 的技术. 二.第一 ...

  7. RMAN_学习笔记2_RMAN Setup配置和监控

    2014-12-23 Created By BaoXinjian

  8. ABAP DEMO

    sap Program DEMO 介绍 Program Description BALVBT01 Example SAP program for displying multiple ALV repo ...

  9. 标准IO库

    IO标准库类型和头文件

随机推荐

  1. 如何解决android studio 运行时中文乱码的问题

    相信很多朋友都会遇到android studio 在MAC OS中运行的时候中文乱码.而在代码编辑的时候正常.经过几天的不断寻找解决办法,终于解决了 比如: Toast.makeText(MainAc ...

  2. 刀哥多线程串行队列gcd-04-dispatch_queue_serial

    串行队列 特点 以先进先出的方式,顺序调度队列中的任务执行 无论队列中所指定的执行任务函数是同步还是异步,都会等待前一个任务执行完成后,再调度后面的任务 队列创建 dispatch_queue_t q ...

  3. iOS8中如何将状态栏的字体颜色改为白色

    网上的一些方法在我这行不通, 比如: UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent ...

  4. FPGA---ucf文件编写

    摘要:本文主要通过一个实例具体介绍ISE中通过编辑UCF文件来对FPGA设计进行约束,主要涉及到的约束包括时钟约束.群组约束.逻辑管脚约束以及物理属性约束. Xilinx FPGA设计约束的分类 Xi ...

  5. 6.Inout双向端口信号处理方法

    Verilog中inout端口的使用方法 (本文中所有Verilog描述仅为展示inout端口的用法,实际描述则需要更丰富的功能描述) Inout端口的使用 在芯片中为了管脚复用,很多管脚都是双向的, ...

  6. 【ConnerStone】SVN代码管理 - 基本使用

    第一步,链接服务器,创建代码管理仓库 第二步,输入服务器的配置,链接服务器(例子是以svn:// 为例子) 第三部 ,链接成功后,SVN的基本界面组成 第四步 从仓库中check out你需要的项目 ...

  7. iOS UIView的简单渐变效果

    这里主要用到了ios4.0以后 UIView的类方法 animateWithDuration: 函数原型包括以下类方法 + (void)animateWithDuration:(NSTimeInter ...

  8. Centering HTML elements larger than their parents

    Centering HTML elements larger than their parents It's not a common problem, but I've run into it a ...

  9. 基于.net mvc的校友录(源程序)

    废话不多说,上程序再说: http://pan.baidu.com/s/11MnLo 我.net mvc4的正式学习时长,其实也就一个多月,期间除去玩游戏.听歌.谈恋爱,也就半个月,大神请轻喷~~ 转 ...

  10. dd面试经历

     HR面:看了我的简历,说fe做的简历就是不一样哈哈好吧,然后随便问了点项目,又问了什么时候可以去实习,就没了.三面:基本数据结构.冒泡排序.数组去重.ie与主流浏览器事件绑定.垂直居中的css实现方 ...