C++基礎知識の回顧

1278 ワード

メモリの管理
メモリ割り当て時のステップ:
  • メモリを割り当てる場所にポインタを向ける
  • .
  • if(p=null)で分配の有無を判断する
  • メモリが切れたらdeleteでメモリを解放する
  • 最後にポインタを空にする
  • //example1
    int * p = new int;
    if(p == NULL)
    {
        //      
        //    
    }
    delete p;
    p = NULL;
    
    
    //example2
    int *p = new int[1000];
    if(p == NULL)
    {
        //      
        //    
    }
    delete []p; //        ,   delete
    p = NULL;   //         
    

    const
    http://blog.csdn.net/Eric_Jo/article/details/4138548
  • constと基本データ型
  • int x=3;            //        ,     
    const int x=3;      //        ,     
    
  • constとポインタタイプ
  • (可変ポインタはconst定義の定数を指すことができず、可変ポインタは変数を指すことができる)
    int const *p;  <=>  const int *p;          ,    const    *p,  *p      , P      。 ①
    int *const p;           ,    const    p,P      。 ②
    int const *const p;  <=>  const int *const p;          p           ,     p           
    
  • 区分の奇淫巧技
  • 覚えやすい方法を言ってint const pとint const pを区別し、*をpointer toと読んで後ろから前へ読む.
    最初のint const*pはp is a pointer to const intと読むことができ、pは定数を指すポインタである
    2番目のint*const pはp is a const pointer to intと読むことができ、pはint型を指す常ポインタである
    *に沿って線を引く方法もあります.
    constが*の左側にある場合、constはポインタが指す変数、すなわちポインタが定数を指すことを修飾するために使用されます.
    constが*の右側にある場合、constは修飾ポインタ自体、すなわちポインタ自体が定数です.