#include<iostream>
int main()
{
using namespace std;
unsigned short int myAge = 5, yourAge = 10;
unsigned short int * pAge = &myAge;
cout << "myAge:\t" << myAge
<< "\t\tyourAge:\t" << yourAge << endl;
cout << "&myAge:\t" << &myAge
<< "\t&yourAge:\t" << &yourAge << endl;
cout << "pAge:\t" << pAge << endl;
cout << "pAge:\t" << *pAge << endl;
cout << "\nReassigning:pAge=&yourAge..." << endl << endl;
pAge = &yourAge;
cout << "myAge:\t" << myAge <<
"\t\tyourAge:\t" << yourAge << endl;
cout << "&myAge:\t" << &myAge
<< "\t&yourAge:\t" << &yourAge << endl;
cout << "pAge:\t" << pAge << endl;
cout << "*pAge:\t" << *pAge << endl;
cout << "\n&pAge:\t" << &pAge << endl;
system("pause");
return 0;
}
输出结果:
数组名是指向数组第一个元素的指针;
局部变量和函数参数位于堆栈中,当然,代码位于代码空间中,而全局变量位于全局名称空间中,寄存器用于内部管理工作,如记录栈顶指针和指令指针,余下的所有内存被作为自由存储区,通常称为堆;
使用关键字new来分配自由存储区中的内存,new的返回值是一个内存地址;
unsigned short int * pPointer=new unsigned short int;
#include<iostream>
int main()
{ using namespace std;
int localVariable=5;
int * pLocal=&localVariable;
int * pHeap=new int;
*pHeap=7;
cout<<"localVariable:"<<localVariable<<endl;
cout<<"*pLocal:"<<*pLocal<<endl;
cout<<"*pHeap:"<<*pHeap<<endl;
delete pHeap;
pHeap=new int;
*pHeap=9;
cout<<"*pHeap:"<<*pHeap<<endl;
delete pHeap;
return 0;
}
//避免迷途指针:
USHORT * pInt=new USHORT;
* pInt=10;
delete pInt;
*pInt=20 //注意:这样做是不对的,应该将指针设置为空(0)或者 重新分配pInt=new USHORT再使用