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的更多相关文章
- JavaScript : Array assignment creates reference not copy
		
JavaScript : Array assignment creates reference not copy 29 May 2015 Consider we have an array var a ...
 - [C++] Copy Control (part 1)
		
Copy, Assign, and Destroy When we define a class, we specify what happens when objects of the class ...
 - std::copy使用方法
		
推荐2个c++函数库,类定义的资料库: http://en.cppreference.com/w/cpp/algorithm/copy http://www.cplusplus.com/referen ...
 - [翻译]Writing Custom DB Engines  编写定制的DB引擎
		
Writing Custom DB Engines 编写定制的DB引擎 FastReport can build reports not only with data sourced from ...
 - HEC-ResSim原文档
		
HEC-ResSim Reservoir System Simulation User's Manual Version 3.1 May 201 ...
 - 数据结构《19》----String容器的三种实现
		
一.序言 一个简单的string 容器到底是如何实现的? 本文给出了 String 的三种从易到难的实现,涉及了 reference counting, copy on write 的技术. 二.第一 ...
 - RMAN_学习笔记2_RMAN Setup配置和监控
		
2014-12-23 Created By BaoXinjian
 - ABAP DEMO
		
sap Program DEMO 介绍 Program Description BALVBT01 Example SAP program for displying multiple ALV repo ...
 - 标准IO库
		
IO标准库类型和头文件
 
随机推荐
- Helloworld模块之内核makefile详解
			
Hello World 模块以及对应的内核makefile详解 hello.c: #include <linux/module.h> //所有模块都需要的头文件 #include < ...
 - Why string is immutable in Java ?
			
This is an old yet still popular question. There are multiple reasons that String is designed to be ...
 - Android 实现子View的状态跟随父容器的状态
			
最近自学着做东西,需要做一个效果,就是我ListView的Item点击下或者选中的时候,我Item里面的各个组件的状态要和我Item的状态保持一直,这样我就可以用XML,去根据组件的不同状态去实现不同 ...
 - DataReader 链接关闭解惑篇
			
不管是啥xxDataReader,都是继承DataReader实现的,所以是有共性的,因此标题就以DataReader为题了. 情况一:DataReader 默认链接不关闭 static void M ...
 - python 删除list中重复元素
			
list = [1,1,3,4,6,3,7] 1. for s in list: if list.count(s) >1: list.remove(s) 2. list2=[] for s in ...
 - file_get_contents函数和curl函数不能用
			
某天file_get_contents()突然不能用了,检查了下php配置文件 allow_url_fopen=on 没问题 各种重启也没用 最后在ssh下执行 chmod 755 /etc/re ...
 - 实体框架 (EF) 入门 => 六、性能注意事项
			
这个还真是复杂,看了看微软的文档,有些根本就看不懂,有些能看懂,但对我这种菜鸟也不会去用. 无从下手啊,前面放了几个链接,挨个试试吧. 一.显式打开连接 这个我测试过,有些时候,需要我们显示打开连接, ...
 - java数据结构和算法------选择排序
			
package iYou.neugle.sort; public class Select_sort { public static void SelectSort(double[] array) { ...
 - Java实现TCP之Echo客户端和服务端
			
Java实现TCP之Echo客户端和服务端 代码内容 采用TCP协议编写服务器端代码(端口任意) 编写客户机的代码访问该端口 客户机按行输入 服务器将收到的字符流和接收到的时间输出在服务器consol ...
 - Windows python 安装 nNumpy、Scipy、matplotlib模块
			
折腾了 很久,总结一些. 首先如果python 是64位,安装32位的numpy ,Scipy,或者matplotlib 模块. 会出现很多问题. 比如当你 在python 导入 Numpy 时,导入 ...