C++ラーニング:malloc/freeとnew/deleteダイナミックメモリ割り当て


C++学習:malloc/freeとnew/delete
概要:C言語では、メモリの申請と解放はmalloc/calloc/realloc/freeで実現するが、C++言語では、以上の関数を使用するだけでなく、new/deleteの形式でメモリの申請と解放を行うことができる.では、この2つの異なる申請と釈放にはいったいどんなメリットとデメリットがあるのだろうか.同じように彼らはスタックメモリを操作しています.またどんな違いがありますか?
ヒント
ブロガー:章飛_906285288ブログアドレス:http://blog.csdn.net/qq_29924041
C言語にはすでにmalloc/freeがありますが、なぜC++にnew/deleteを追加するのでしょうか.
  • mallocとfreeはC++/Cが提供する標準ライブラリ関数であり、new/deleteはC++演算子である.注意:1つは関数であり、1つは演算子であり、これはとっくに彼らが処理する時期が異なる
  • である.
  • C言語の関数にはプログラムが含まれており、いずれも静的チェーン編成のプロセスであり、C++プログラムは動的コンパイルを強調している(注意:コンパイル時、強調実行時)
  • 内部データ型(基本データ型)以外のオブジェクトの場合、malloc/freeだけでは動的オブジェクトの要件を満たすことができない.これは実行時であるため、オブジェクトの作成と消滅と同時にコンストラクション関数とコンストラクション関数を呼び出す必要がある.mallocとfreeはライブラリ関数であるため、コンパイラの制御範囲内ではない.注意:new/deleteはオペレータであり、関数
  • ではありません.
  • 基本データ型(内部データ型)については、構築および解析のプロセスがないため、new/deleteおよびmalloc/freeの効果は等価な
  • である.
  • は、場合によっては(内部データ型)、malloc/freeおよびnew/deleteの間で相互に呼び出すことができる(使用を推奨しない)が、freeが「newが作成したオブジェクトを削除した場合」、構造関数を実行できないため、プログラム全体のエラーが発生する可能性があるため、
  • malloc/freeおよびnew/deleteは、ペアで使用する
  • である必要があります.
  • malloc/freeとnew/deleteはすべてスタックメモリを操作して、注意:スタックメモリの申請解放はすべてシステムが自分で制御する
  • です
  • 実行時操作オブジェクトはコンパイル時よりも優れているため、C++ではnew/deleteがmalloc/freeよりも優れているが、malloc/freeを捨てることはできないのは、C++がCを呼び出すプログラムを必要とする場合があるため、
      : C           :
         char *array = (char*)malloc(10);
         //             ,         10   ,        10   
         C++ :
         char temp[20];
         char *array = new array(strlen(temp)+1);
         strcpy(array,temp);
             ,             
    
  • を捨てることはできないからである.
    サンプルコード:
    /*
     * ===========================================================================
     *
     *       Filename:  snewstrct.cpp
     *    Description:  
     *        Version:  1.0
     *        Created:  2017 07 03  22 21 16 
     *       Revision:  none
     *       Compiler:  gcc
     *         Author:   (), 
     *        Company:  
     *
     * ===========================================================================
     */
    
    #include
    #include
    #include
    #include
    using namespace::std;
    
    /* *
     *   (      )
     * */
    struct inflatable{
      char name[20];//    
      float volume;//  
      double price; //  
    };
    
    
    void show_inflatable(const inflatable* inf){
      cout << inf->name <cout << inf->volume <cout << inf->price <int main(int argc,char* argv[]){
      //  new/delete
      inflatable* inf = new inflatable;
      cout << "Enter name of inflatable item:";
      cin.get(inf->name,20);//(        ,  ->)
      cout << "Enter volumn in cubic feet:";
      cin >> (*inf).volume;   //          (  .   )
      cout <<"Enter price:";
      cin >> inf->price;
    
      show_inflatable(inf);
    
      //  free malloc   (    )
      inflatable* inf_t = (inflatable*)malloc(sizeof(inflatable));
      strcpy(inf_t->name,"   ");//     copy  
      inf_t->volume = 1.6;
      inf_t->price = 35;
      show_inflatable(inf_t);
    
    
    
      free(inf_t);
      //free(inf);      ,                  ,(          )
      delete inf;
    
      return 0;
    }

    new/deleteが動的に実行される場合の利点のコード表現:
    /*
     * ===========================================================================
     *
     *       Filename:  deletearray.cpp
     *    Description:  
     *        Version:  1.0
     *        Created:  2017 07 03  22 40 59 
     *       Revision:  none
     *       Compiler:  gcc
     *         Author:   (), 
     *        Company:  
     *
     * ===========================================================================
     */
    
    #include
    #include
    #include
    using namespace::std;
    
    
    /* *
     *  :  name new     ,                  ,         
      array            .           
     * */
    char* getname(){
      char array[20];
      cout<<"enter getname:";
      cin.get(array,sizeof(array));
      //new             ,                
      char *name = new char[strlen(array)+1];//+1              ,  new      ,      delete []
      strcpy(name,array);  // array    copy name    
      return name;
    }
    
    
    int main(int argc,char *argv[]){
    
      //C          ,      ,         20      ,         20   
      //               
      char array[20];
      cout << "Enter a name:";
      cin.get(array,sizeof(array));
      cout<< "array:"<<array<cin.get();//             
    
      char *name;
      name = getname();
    
      cout<delete [] name;
    
      return 0;
    }