编写c++程序的优良习惯

代码
// HighQCPP.cpp : 定义控制台应用程序的入口点。
//

#include 
"stdafx.h"
#include 
<iostream>
using namespace std;

void GetMemory(char *p);
char *GetMemory(void);
void GetMemory(char **p,int num);
void test();
void test2();
void main()
{
    
/*编写优良风格的c++程序*/

    
/*    
    1.bool flag 与"零值"比较的if语句
    
*/
    
bool flag=true;
    
if (flag)//优良风格
    {
        cout
<<""<<endl;
    }
    
if (!flag)//优良风格
    {
        cout
<<""<<endl;
    }
    
if (flag==true)//不良风格
    {
        cout
<<""<<endl;
    }
    
if (flag==false)//不良风格
    {
        cout
<<""<<endl;
    }
    
if (flag==1)//不良风格
    {
        cout
<<""<<endl;
    }
    
if (flag!=1)//不良风格
    {
        cout
<<""<<endl;
    }
    cout
<<"********************************************************************************"<<endl;
    
/*    
    2.写出float x与"零值"比较的if语句
    
*/
    
//优良风格
    const float EPSINON=0.00001;
    
float x=0;
    
if (x>=-EPSINON&&x<=EPSINON) 
    {
         cout
<<"不可以将float变量用'=='或'!='与数字比较,应该转化为'>='或'<='形式"<<endl;
    }
    
//不良风格
    if(x==0.0)    
    {
        cout
<<"这种写法编译是成功,但是不推荐"<<endl;
    }
    cout
<<"********************************************************************************"<<endl;
    
/*
    3.写出char* p 与"零值"比较的if语句
    
*/    
    
char* p="aaa";
    
//优良风格
    if (p==NULL)
    {
        
        
//do sth.
    }
    
if (p!=NULL)
    {
        cout
<<"优良风格"<<endl;
        
//do sth.
    }
    
//不良风格
    if (p==0)
    {
        
//
    }
    
if (p!=0)
    {
        cout
<<"不良风格"<<endl;
    }
    cout
<<"********************************************************************************"<<endl;
    
/*
    4.在32位windows机器上计算如下值的sizeof
    
*/
    
char str[]="hello";
    
char *pp=str;
    
int n=10;
    cout
<<"字符串str的sizeof的值为:"<<sizeof(str)<<endl;
    cout
<<"指针pp的sizeof值为:"<<sizeof(p)<<endl;
    cout
<<"整形变量n的sizeof值为:"<<sizeof(n)<<endl;
    
char *str2=new char[100]; //新开辟了内存
    void *pp2=malloc(100);
    cout
<<"new出来的str22的sizeof的值为:"<<sizeof(str2)<<endl;
    cout
<<"指针pp2的sizeof值为:"<<sizeof(pp2)<<endl;
    cout
<<"********************************************************************************"<<endl;
    
/*
    5.关于内存的思考题:
    
*/
    
//运行下面这段程序,会发现程序崩溃,因为GetMemory不能传递动态内存
    /*
    char *str3=NULL;
    GetMemory(str3);
    strcpy(str3,"hello world");//程序崩溃
    cout<<"运行后的结果为"<<str3<<endl;
    
*/
    
//运行下面一段代码,发现输出结果是乱码,因为GetMemory()返回的是指向"栈内存"的指针,该指针不是NULL,但是其原先的内容被清除,新内容不可知
    char *str4=NULL;
    str4
=GetMemory();
    cout
<<"结果正常吗:"<<str4<<endl;//会发现是乱码
    /*
    运行下面代码发现能输出"helloworld",但是内存泄露了  
    
*/
    test();
    
/*
    篡改了动态内存区的内容,产生野指针
    
*/
    test2();
    system(
"pause");

}
//GetMemory不能传递动态内存,这种写法有问题
void GetMemory(char *p)
{
    p
=(char*)malloc(100);
}
char *GetMemory(void)
{
    
char p[]="hell world";
    
return p;
}
void GetMemory(char **p,int num)
{
    
*p=(char*)malloc(num);
}
void test()
{
    
char *str=NULL;
    GetMemory(
&str,100);
    strcpy(str,
"hell world");
    cout
<<"输出了,但是内存泄露了:"<<str<<endl;
}
void test2()
{
    
char *str=(char*)malloc(100);
    strcpy(str,
"hello world");
    free(str);
//str成为野指针
    delete(str);
    
if (str!=NULL)//继续执行了
    {
        strcpy(str,
"hahahah");
        cout
<<"结果为"<<str<<endl;
    }
}

原文地址:https://www.cnblogs.com/guanjie20/p/1667135.html