数组、指针初始化与非初始化
在数组或指针定义后未初始化的情况,如:
int pInt1[2];
int *pInt2;
对其调用往往会出现意想不到的情况(注意:不一定会报错),现在做个小例子,测试一下到底会出现什么情况。
(一)VC9.0,Debug模式下调试:
1 #include "stdafx.h" 2 #include <vector> 3 #include <iostream> 4 #include <assert.h> 5 6 using namespace std; 7 int _tmain(int argc, _TCHAR* argv[]) 8 { 9 10 int pInt[2]={0};//数组初始化 11 int pInt1[2]; //数组未初始化 12 int *pInt2; //指针未初始化 13 int *pInt3=NULL;//指针初始化为空 14 15 16 cout<<"/* address: */"<<endl; 17 cout<<"address pInt: "<<pInt<<endl; 18 cout<<"address pInt1: "<<pInt1<<endl; 19 //cout<<"address pInt2: "<<pInt2<<endl;//报错,pInt2未初始化 20 cout<<"address pInt3: "<<pInt3<<endl; 21 cout<<endl; 22 23 cout<<"/*int pInt[2]={0};*/"<<endl; 24 cout<<"pInt[0]="<<pInt[0]<<endl;//0 25 cout<<"pInt[1]="<<pInt[1]<<endl;//0 26 cout<<"pInt[2]="<<pInt[2]<<endl;//数组越界,随机数 27 cout<<"pInt[3]="<<pInt[3]<<endl;//数组越界,随机数 28 cout<<endl; 29 30 31 cout<<"/* int pInt1[2]; */"<<endl; 32 cout<<"pInt[0]="<<pInt1[0]<<endl;//数组未初始化,随机数 33 cout<<"pInt[1]="<<pInt1[1]<<endl;//数组未初始化,随机数 34 cout<<"pInt[2]="<<pInt1[2]<<endl;//数组未初始化,越界,随机数 35 cout<<"pInt[3]="<<pInt1[3]<<endl;//数组未初始化,越界,随机数 36 cout<<endl; 37 cout<<endl; 38 39 //pInt2调用全部报错 40 //cout<<"/* int *pInt2; */"<<endl; 41 //cout<<"pInt2[0]="<<pInt2[0]<<endl; 42 //cout<<"pInt2[0]="<<pInt2[1]<<endl; 43 //cout<<endl; 44 45 //pInt3调用全部报错 46 //cout<<"int *pInt3=NULL"<<endl; 47 //cout<<pInt3[0]<<endl; 48 //cout<<pInt3[1]<<endl; 49 //cout<<endl; 50 51 return 0; 52 }
运行结果:
(二)VC9.0,Release 模式下调试:
1 #include "stdafx.h" 2 #include <vector> 3 #include <iostream> 4 #include <assert.h> 5 6 using namespace std; 7 int _tmain(int argc, _TCHAR* argv[]) 8 { 9 10 int pInt[2]={0};//数组初始化 11 int pInt1[2]; //数组未初始化 12 int *pInt2; //指针未初始化 13 int *pInt3=NULL;//指针初始化为空 14 15 16 cout<<"/* address: */"<<endl; 17 cout<<"address pInt: "<<pInt<<endl; 18 cout<<"address pInt1: "<<pInt1<<endl; 19 cout<<"address pInt2: "<<pInt2<<endl; 20 cout<<"address pInt3: "<<pInt3<<endl; 21 cout<<endl; 22 23 cout<<"/*int pInt[2]={0};*/"<<endl; 24 cout<<"pInt[0]="<<pInt[0]<<endl;//0 25 cout<<"pInt[1]="<<pInt[1]<<endl;//0 26 cout<<"pInt[2]="<<pInt[2]<<endl;//数组越界,随机数 27 cout<<"pInt[3]="<<pInt[3]<<endl;//数组越界,随机数 28 cout<<endl; 29 30 31 cout<<"/* int pInt1[2]; */"<<endl; 32 cout<<"pInt[0]="<<pInt1[0]<<endl;//数组未初始化,随机数 33 cout<<"pInt[1]="<<pInt1[1]<<endl;//数组未初始化,随机数 34 cout<<"pInt[2]="<<pInt1[2]<<endl;//数组未初始化,越界,随机数 35 cout<<"pInt[3]="<<pInt1[3]<<endl;//数组未初始化,越界,随机数 36 cout<<endl; 37 cout<<endl; 38 39 //pInt2调用全部报错 40 cout<<"/* int *pInt2; */"<<endl; 41 cout<<"pInt2[0]="<<pInt2[0]<<endl;//指针未初始化,指向随机数 42 cout<<"pInt2[0]="<<pInt2[1]<<endl;//指针未初始化,指向随机数 43 cout<<endl; 44 45 //pInt3调用全部报错 46 //cout<<"int *pInt3=NULL"<<endl; 47 //cout<<pInt3[0]<<endl; 48 //cout<<pInt3[1]<<endl; 49 //cout<<endl; 50 51 return 0; 52 }
运行结果:
综上,
数组未初始化时、或者在数组越界调用其元素,在debug和release模式下,虽然均可以运行,但是出现均为随机数,属于不可空错误;
指针未初始化时,调用其指向元素,bebug下程序无法通过;release下可以运行,单数由于指针指向地址为随机分配,所以其指向的元素也是随机数,会出现不可预知错误。
所以数组、指针定义时,一定要记得初始化。指针指向的new对象释放后,一定要将指针置零,防止野指针。
int *pInt=new int;
....
delete pInt;
pInt=NULL;
知识就是力量!