动手测试C++中指针与引用作为函数参数的行为
来源:互联网 发布:淘宝女士休闲连衣裙 编辑:程序博客网 时间:2024/05/21 19:31
半路转行IT,初学C++,用MFC帮哥们编一个专业上的绘图设计计算的软件时,参考微软给的几个示例源代码参考着在做。由于基础不好,老是被里面的指针和引用弄得有点头晕眼花。特别是函数的参数,有时候是指针,有时候是普通变量,有时候是引用,让人凌乱。于是写了一段小代码,初步测试一下。
测试方法:三个函数,分别是把一个整数变量i传递给自己的参数然后进行+1的操作。DoNumber1的参数是一个引用,DoNumber2的参数是整数变量,DoNumber3的参数是一个int*指针。第三个函数略有不同是拿 *number去+1。
运行结果:
The number is: 5
After method 1 the number becomes: 6
After method 2 the number becomes: 6
The address of the pointer is: 0113FC94
The int value the pointer points to is: 6
The address of the point is :0113FC94
The int value the pointer points to is: 7
After method 3 the number becomes: 7
结论:如果参数是引用,函数里对参数number的操作会改变原始变量i;如果参数是变量,则不会改变传递给参数的变量,估计这时候创建了一个新的变量获取了原始变量的值;如果参数是指针,对*number的操作也会改变原始参数i的值。
另类测试:把第三个函数中的"*number += 1"之前加一行:"number += 1",即把下述代码中注释掉的那行加入。
运行结果:
The number is: 5
After method 1 the number becomes: 6
After method 2 the number becomes: 6
The address of the pointer is: 00AFF964The int value the pointer points to is: 6
The address of the pointer is: 00AFF968
The int value the pointer points to is: -858993459
After method 3 the number becomes: 6
结论:由于number+=1把变量number的地址更改了,后面的*number +=1 已经不会修改到i的值。
困惑:为什么对地址+1,结果得到的新地址比原来的大4呢?在新的地址里的数字为什么是-858993459呢?在做那个另类测试时,当退出CMD框时,跳出来一个runtime error,"Run-Time Check Failure #2 - Stack around the variable i was corrupted",为什么呢,不过说明了一个问题,指针乱用会很危险。。等找到答案了再来更新。
插曲:作为一个初学者,毕竟土鳖,一开始没有在前面声明那三个函数,结果报错;然后,在用cout和endl的时候忘了namespace这茬了。。。
#include <iostream>void DoNumber1(int& number);void DoNumber2(int number);void DoNumber3(int* number);int main(void){int i = 5;std::cout<<"The number is: "<<i<<std::endl;DoNumber1(i);std::cout<<"After method 1 the number becomes: "<<i<<std::endl;DoNumber2(i);std::cout<<"After method 2 the number becomes: "<<i<<std::endl;DoNumber3(&i);std::cout<<"After method 3 the number becomes: "<<i<<std::endl;system("pause");return 0;}void DoNumber1(int& number){number = number + 1;}void DoNumber2(int number){number = number + 1;}void DoNumber3(int* number){std::cout<<"The address of the pointer is: "<<number<<std::endl;std::cout<<"The int value the pointer points to is: "<<*number<<std::endl;//number += 1;*number +=1;std::cout<<"when the parameter is a pointer"<<number<<std::endl;std::cout<<"The int value the pointer points to is: "<<*number<<std::endl; }
- 动手测试C++中指针与引用作为函数参数的行为
- C指针作为函数参数引用
- 指针作为函数的参数,引用作为函数的参数
- 指针的引用作为函数参数有啥用?
- C语言中“指针”作为“函数参数”时的注意事项
- c语言中指针变量作为函数的参数
- C语言中指针变量作为函数参数和一般变量作为函数参数的区别
- C++指针,引用,常引用作为函数的参数
- 【C++】指针作为函数的参数
- 图解C/C++中函数参数的值传递、指针传递与引用传递
- 图解C/C++中函数参数的值传递、指针传递与引用传递
- C/C++中作为函数参数的 i++ 与 ++i
- C++ 引用与引用作为函数的参数
- 指针和引用作为函数传递参数
- c++中指针的引用 *&作为参数传递
- 函数参数指针与引用的区别
- (转载)c语言中函数指针作为参数
- “引用作为函数参数”与 “引用作为函数返回值”
- PS各种操作的快捷键---捷键
- 使用JConsole监控进程、线程、内存、cpu、类情况
- Spring-security不能重复登录
- pads2007 LISENCE 报错解决方案
- GDAL-OGR对shp文件的操作
- 动手测试C++中指针与引用作为函数参数的行为
- 关于python中的setup.py
- JConsole 的升级版 VisualVM 入门指南
- shell中的大括号和小括号
- wordpress彩色标签云代码实现两大方法
- Filter(五)FilterChain执行顺序
- poj-2251-Dungeon Master(bfs)
- HugePages on Oracle Linux 64-bit
- spring+hibernate自动生成数据库表结构