转自: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. DTW

    DTW主要是应用在孤立词识别的算法,用来识别一些特定的指令比较好用,这个算法是基于DP(动态规划)的算法基础上发展而来的.这里介绍语音识别就先介绍下语音识别的框架,首先我们要有一个比对的模版声音,然后 ...

  2. “猜你喜欢”是怎么猜中你心思的?

    文/Joseph A. Konstan & John Riedl)如今,到网上购物的人已经习惯了收到系统为他们做出的个性化推荐.Netflix 会推荐你可能会喜欢看的视频.TiVo 会自动把节 ...

  3. poj 1383 Labyrinth

    题目连接 http://poj.org/problem?id=1383 Labyrinth Description The northern part of the Pyramid contains ...

  4. hdu 1874 畅通工程续

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过 ...

  5. 通过 CALayer 修改 UIImageView 的界面属性

    界面属性的修改是每一个开发者必须知道的,为什么我就记不住呢, shit, 又耽误了时间,为了防止再找不到,特把一些常用的 CALayer属性记在这里,顺便分享 1.设置阴影 1 imageView.l ...

  6. Javascript Date Format

    // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占 ...

  7. Java下Web MVC的领跑者:SpringMVC

    比较常用的MVC框架有Struts 和 SpringMVC. Struts 是Java Web MVC框架中曾经不争的王者.经过长达九年的发展,Struts占有了MVC框架中最大的市场份额.但是Str ...

  8. 关于ASCII、GB231、GBK、UTF-8/UTF8、ANSI、unicode的学习笔记

    继续上次的学习内容,写一些自己学习的笔记吧!总是觉得没有笔记的学习总是不那么踏实,我承认自己是个记忆力很差的人,特别羡慕那些可以把自己学过的东西记得很牢靠的人.哎!可惜我不是,那只能做出来点东西,就算 ...

  9. 利用 js 实现弹出蒙板(model)功能

    关于 js 实现一个简单的蒙板功能(model) 思路: 创建一个蒙板, 设置蒙板的堆叠顺序保证能将其它元素盖住 position: absolute; top: 0; left: 0; displa ...

  10. BZOJ 1001: [BeiJing2006]狼抓兔子 最小割

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1001 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓 ...