malloc(0)

890阅读 0评论2015-07-08 seuqyr
分类:C/C++

char * ptr;
ptr=(char*)malloc(0);
if(ptr==NULL)
{
cout<<"it is a null pointer"<<endl;

}
else
{
cout<<"it is a non-null pointer"<<endl;
cout<<"the length is "<<sizeof(ptr)<<endl;
cout<<"the strlen is "<<strlen(ptr)<<endl;
}


输出结果:it is a non-null pointer
                the length is 4
            
    the strlen is 16
把0传给malloc,可以得到一个合法的指针。
上一篇:C++ cast转换
下一篇:重要的字符串处理函数