![]() |
#2
yuccn2013-03-25 22:11
|

#include <iostream>
using namespace std;
int *Inti()
{
int *p;
if(p=new int (5))
return p;
return NULL;
}
int main()
{
int *o;
int *q;
q=o=Inti();
cout<<o<<" "<<*o<<endl;
delete q;
cout<<o<<" "<<*o<<endl;
system("
using namespace std;
int *Inti()
{
int *p;
if(p=new int (5))
return p;
return NULL;
}
int main()
{
int *o;
int *q;
q=o=Inti();
cout<<o<<" "<<*o<<endl;
delete q;
cout<<o<<" "<<*o<<endl;
system("
函数里面定义了指针p并通过new申请了内存。
p是个局部变量。
p申请的内存却在main函数里用的不亦乐乎。。
是new的功能还是说是返回指针的功能?
还有new和malloc()有什么区别呢?
还有。不违反作用域么。。
(虽然数据结构书里写的不亦乐乎,实际也可行,但搞不懂啊。。)