#include <iostream>
using namespace std;
int fun1() {
int i = 1;
cout<<"fun1 i address"<<&i<<endl;
return i;//ok,返回值是i值得拷贝
}
int *fun2() {//指针类型的函数
int i = 2;
int *ip = &i;
cout<<"fun2 i address"<<ip<<endl;
return ip; // Wrong!返回值是ip指针的拷贝,但该地址在函数结束后会释放变得无效
}
int main() {
int r1 = fun1();
cout<<"main fun1 return i address"<<&r1<<endl;
cout << r1 << endl; // 1
int *r2 = fun2();
cout<<"main fun2 return i address"<<r2<<endl;
//这里有可能出错:具体看对应的内存是否被覆盖,但总之该内存已无效
cout << *r2 << endl;//0
return 0;
}
输出:
fun1 i address0x7ffc49e9b69c main fun1 return i address0x7ffc49e9b6b4 1 fun2 i address0x7ffc49e9b694 main fun2 return i address0x7ffc49e9b694 0
我们在看一个对象的例子:
#include <iostream>
using namespace std;
class Point {
public:
Point(int a,int b):x(a),y(b){}
int getX();
void setX(int x);
private:
int x,y;
};
int Point::getX(){
return x;
}
void Point::setX(int a) {
x = a;
}
Point func(int x) {
Point p(x,100);
cout<<"func1 p address:"<<&p<<endl;
return p;//ok,发生一次Point拷贝
}
Point *func2(int x) {//指针函数
Point p(x,200);
cout<<"func2 p address:"<<&p<<endl;
return &p;//wrong,返回值是p地址的拷贝,但该地址在函数结束后会被释放变得无效
}
main() {
Point p = func(1);
cout<<"main return p address:"<<&p<<endl;
cout<<"main return p x:"<<p.getX()<<endl;
Point *p2 = func2(2);
cout<<"main return p address:"<<p2<<endl;
cout<<"main return p x:"<<p2->getX()<<endl;
}
编译的时候会有一个警告:
test88.cpp: In function ‘Point* func2(int)’: test88.cpp:26:9: warning: address of local variable ‘p’ returned [-Wreturn-local-addr] Point p(x,200); ^
输出:
func1 p address:0x7fff0f005270 main return p address:0x7fff0f005290 main return p x:1 func2 p address:0x7fff0f005270 main return p address:0x7fff0f005270 main return p x:6299776
int *fun3(){
static int i = 5;
cout<<"fun3 i address:"<<&i<<endl;
return &i;
}
int main() {
int *r1 = fun3();
cout<<"main return i address:"<<r1<<endl;
cout<<*r1<<endl;
}
输出:
fun3 i address:0x602078 main return i address:0x602078 5
#include <iostream>
using namespace std;
int fun1() {
int i = 1;
cout<<"fun1 i address"<<&i<<endl;
return i;//ok,返回值是i值得拷贝
}
int *fun2() {//指针类型的函数
int i = 2;
int *ip = &i;
cout<<"fun2 i address"<<ip<<endl;
return ip; // Wrong!返回值是ip指针的拷贝,但该地址在函数结束后会释放变得无效
}
int main() {
int r1 = fun1();
cout<<"main fun1 return i address"<<&r1<<endl;
cout << r1 << endl; // 1
int *r2 = fun2();
cout<<"main fun2 return i address"<<r2<<endl;
//这里有可能出错:具体看对应的内存是否被覆盖,但总之该内存已无效
cout << *r2 << endl;//0
return 0;
}
输出:
fun1 i address0x7ffc49e9b69c main fun1 return i address0x7ffc49e9b6b4 1 fun2 i address0x7ffc49e9b694 main fun2 return i address0x7ffc49e9b694 0
我们在看一个对象的例子:
#include <iostream>
using namespace std;
class Point {
public:
Point(int a,int b):x(a),y(b){}
int getX();
void setX(int x);
private:
int x,y;
};
int Point::getX(){
return x;
}
void Point::setX(int a) {
x = a;
}
Point func(int x) {
Point p(x,100);
cout<<"func1 p address:"<<&p<<endl;
return p;//ok,发生一次Point拷贝
}
Point *func2(int x) {//指针函数
Point p(x,200);
cout<<"func2 p address:"<<&p<<endl;
return &p;//wrong,返回值是p地址的拷贝,但该地址在函数结束后会被释放变得无效
}
main() {
Point p = func(1);
cout<<"main return p address:"<<&p<<endl;
cout<<"main return p x:"<<p.getX()<<endl;
Point *p2 = func2(2);
cout<<"main return p address:"<<p2<<endl;
cout<<"main return p x:"<<p2->getX()<<endl;
}
编译的时候会有一个警告:
test88.cpp: In function ‘Point* func2(int)’: test88.cpp:26:9: warning: address of local variable ‘p’ returned [-Wreturn-local-addr] Point p(x,200); ^
输出:
func1 p address:0x7fff0f005270 main return p address:0x7fff0f005290 main return p x:1 func2 p address:0x7fff0f005270 main return p address:0x7fff0f005270 main return p x:6299776
int *fun3(){
static int i = 5;
cout<<"fun3 i address:"<<&i<<endl;
return &i;
}
int main() {
int *r1 = fun3();
cout<<"main return i address:"<<r1<<endl;
cout<<*r1<<endl;
}
输出:
fun3 i address:0x602078 main return i address:0x602078 5