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标准库类型和头文件
随机推荐
- 苹果HomeKit如何牵动全国智能硬件格局
苹果HomeKit如何牵动全国智能硬件格局 2014-06-23 15:48 发表 系统分类:消费电子 自定义分类:默认 标签:智能家居 苹果在6月的WWDC开发者大会上发布了包括 ...
- Windows Phone 8.1SDK新特性预览
前言 Windows Phone 8.1的预览版将在近期推送,WP 8.1的SDK也已经进入到RC阶段,可以从这里安装.本次更新的SDK被直接集成到了VS2013Update2里面,不再是单独的 ...
- JVM学习总结五(番外)——JConsole
之前本来打算结合自己写的小程序来介绍JConsole和VirtualVM的使用的,但是发现很难通过一个程序把所有的场景都体现出来,所以还是决定用书中的典型小例子来讲更加清晰. 一.JConsole的基 ...
- 4.Knockout.Js(事件绑定)
前言 click绑定在DOM元素上添加事件句柄以便元素被点击的时候执行定义的JavaScript 函数.大部分是用在button,input和连接a上,但是可以在任意元素上使用. 简单示例 <h ...
- win7虚拟打印驱动开发注意事项
win7虚拟打印驱动开发注意事项 通过控制面板安装遇到以下问题:错误1.提示“Printer driver was not installed. Operation could not be comp ...
- Labview实现频率调制(FM)
Labview实现频率调制(FM) 频率调制的原理: 自己的实现为三角函数分解 根据这个公式在Labview中连线则可以得到最终的波形输出 实现效果 从频域图中可以看出,载波信号的频率被调制,原本为双 ...
- C++类中的this指针的作用
1.我们知道C++的类成员函数中,默认都隐含了一个this指针,标识调用该成员函数的对象 2.为什么需要有一个this指针呢?C++设计这个机制的初衷是什么呢? 我们知道,普通的C++类,其成员函数是 ...
- Notes of the scrum meeting(11/3)
meeting time:19:30~20:00p.m.,November 3th,2013 meeting place:20号公寓楼前 attendees: 顾育豪 ...
- Eigen库实现简单的旋转、平移操作
本来课程要求用GUI界面来实现Eigen的旋转.平移操作的,但是接触GUI编程时间太短,虽然要求很简单,但是做了几天还是没有完成.就把命令行下面的简单的贴一下吧. main.cpp #include ...
- 向Array中添加希尔排序
希尔排序思路 我们在第 i 次时取gap = n/(2的i次方),然后将数组分为gap组(从下标0开始,每相邻的gap个元素为一组),接下来我们对每一组进行直接插入排序. 希尔排序实现 Functio ...