转自: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. 刀哥多线程同步任务作用gcd-07-sync_task

    同步任务的作用 同步任务,可以让其他异步执行的任务,依赖某一个同步任务 例如:在用户登录之后,再异步下载文件! - (void)gcdDemo1 { dispatch_queue_t queue = ...

  2. Python 以正确的宽度在盒子中居中打印一个字符

    注意://为整除的意思 代码: # -*- coding:UTF-8 -*- sentence = input("Sentence:") screen_width = 80 tex ...

  3. hdu 4941 Magical Forest

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4941 Magical Forest Description There is a forest can ...

  4. 【转】Git常用命令备忘

    Git配置 git config --global user.name "robbin" git config --global user.email "fankai@g ...

  5. 如何配置DNS服务器(局域网——域名指向某个IP地址)

    单击“开始”,指向“管理工具”,然后单击“DNS”,打开 DNS 管理器.   如有必要,向管理单元添加适用的服务器,然后连接该服务器.在控制台树中,单击适用的 DNS 服务器.   在“操作”菜单上 ...

  6. cameralink---格式 概要清晰理解

    DS90CR288 和DS90CR286共同组成LVDS收发对, LVDS: 4对数据+1对时钟 TTL: 时钟线1:clk 并行总线共27根: 同步线2:一根场同步,一根行同步 空闲: 1 数据线2 ...

  7. Protocol-SPI协议

    说明.文章摘自:SPI协议及其工作原理浅析 http://bbs.chinaunix.net/thread-1916003-1-1.html 一.概述. SPI,Serial Peripheral I ...

  8. js event bubble and capturing

    Bubble: pppppp Capturing pppppp Mix pppppp To Stop Bubble pppppp // JS Bin

  9. 轻松解决在一个虚拟主机上运行多个 ASP.NET 网站应用

    不知道有没有朋友像我一样会遇到这样一个问题: 在网上购买 .NET 空间,由于虚拟主机的限制,你并不能把某个目录设为一个独立的应用,或者一些价格比较高的空间,虽然可以设置,但数量也是有限的.这个问题导 ...

  10. “我爱淘”第二冲刺阶段Scrum站立会议2

    完成任务: 对发布页面优化了一下,并将登陆的功能实现了一点,就是还没有实现注册的功能 . 计划任务: 在客户端实现分类功能,通过学院的分类查看书籍. 遇到问题: 自动将数据库的内容返回到客户端.