更新时间:2016-05-17 22:30:25浏览次数:1+次
#include "iostream"
#include "windows.h"
using namespace std;
/*
void swap(int p,int q)
{
int temp;
temp=p;
p=q;
q=temp;
}*/
void swap2(int *p,int *q)
{
int *temp;
temp=(int *)malloc(10*sizeof(int));//新添加的代码
*temp=*p;
*p=*q;
*q=*temp;
}
void swap3(int *p,int *q)
{
int *temp;
temp=p;
p=q;
q=temp;
}
void swap4(int &p, int &q)
{
int temp;
temp=p;
p=q;
q=temp;
}
void swap5(int *p,int *q)
{
int temp;
temp=*p;
*p=*q;
*q=temp;
}
int main()
{
int a=1,b=2;
int a1=3,b1=4;
int a2=5,b2=6;
int a3=7,b3=8;
//swap(a,b);
swap2(&a1,&b1);
swap3(&a2,&b2);
swap4(a3,b3);
swap5(&a,&b);
cout<<a<<" "<<b<<endl;
cout<<a1<<" "<<b1<<endl;
cout<<a2<<" "<<b2<<endl;
cout<<a3<<" "<<b3<<endl;
system("pause");
return 0;
}
运行结果:temp=(int *)malloc(10*sizeof(int));
在这里说一句,对于C/C++语言,由于指针泄露而使得程序不能运行。因此,要注意指针的问题!尤其是在数据结构中,像链表等,对指针的运用太多了,因此也容易出错。大家要注意这个指针内存泄漏问题!相关资讯