<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # C++ 內存管理:`new`和`delete` > 原文: [https://www.programiz.com/cpp-programming/memory-management](https://www.programiz.com/cpp-programming/memory-management) #### 在本文中,您將學習使用`new`和`delete`操作在 C++ 中有效地管理內存。 [數組](/cpp-programming/arrays "C++ Arrays")可用于存儲多個同類數據,但是使用數組存在嚴重缺陷。 聲明數組時應分配內存,但在大多數情況下,直到運行時才能確定所需的確切內存。 在這種情況下,最好的做法是聲明一個具有最大可能所需內存的數組(聲明一個預期具有最大可能大小的數組)。 不利的一面是未使用的內存被浪費了,不能被任何其他程序使用。 為了避免浪費內存,您可以在 C++ 中使用`new`和`delete`運算符動態分配運行時所需的內存。 * * * ## 示例 1:C++ 內存管理 **C++ 程序,用于存儲`n`個學生的 GPA,并顯示在其中`n`是用戶輸入的學生人數。** ```cpp #include <iostream> #include <cstring> using namespace std; int main() { int num; cout << "Enter total number of students: "; cin >> num; float* ptr; // memory allocation of num number of floats ptr = new float[num]; cout << "Enter GPA of students." << endl; for (int i = 0; i < num; ++i) { cout << "Student" << i + 1 << ": "; cin >> *(ptr + i); } cout << "\nDisplaying GPA of students." << endl; for (int i = 0; i < num; ++i) { cout << "Student" << i + 1 << " :" << *(ptr + i) << endl; } // ptr memory is released delete [] ptr; return 0; } ``` **輸出** ```cpp Enter total number of students: 4 Enter GPA of students. Student1: 3.6 Student2: 3.1 Student3: 3.9 Student4: 2.9 Displaying GPA of students. Student1 :3.6 Student2 :3.1 Student3 :3.9 Student4 :2.9 ``` 在此程序中,僅動態聲明存儲`num`(由用戶輸入)數量的浮點數據所需的內存。 * * * ## `new`運算符 ```cpp ptr = new float[num]; ``` 上面程序中的此表達式將[指針](/cpp-programming/pointers "C++ Pointers")返回到剛好足以容納`num`個浮點數據的內存部分。 * * * ## `delete`運算符 使用`new`運算符分配內存后,應將其釋放回操作系統。 如果程序使用`new`占用大量內存,則系統可能會崩潰,因為操作系統沒有可用的內存。 以下表達式將內存返回給操作系統。 ```cpp delete [] ptr; ``` 方括號`[]`表示數組已刪除。 如果您需要刪除單個對象,則無需使用方括號。 ```cpp delete ptr; ``` * * * ## 示例 2:C++ 內存管理 **面向對象的方法來處理 C++ 中的上述程序。** ```cpp #include <iostream> using namespace std; class Test { private: int num; float *ptr; public: Test() { cout << "Enter total number of students: "; cin >> num; ptr = new float[num]; cout << "Enter GPA of students." << endl; for (int i = 0; i < num; ++i) { cout << "Student" << i + 1 << ": "; cin >> *(ptr + i); } } ~Test() { delete[] ptr; } void Display() { cout << "\nDisplaying GPA of students." << endl; for (int i = 0; i < num; ++i) { cout << "Student" << i+1 << " :" << *(ptr + i) << endl; } } }; int main() { Test s; s.Display(); return 0; } ``` 該程序的輸出與上述程序相同。 創建對象`Test`時,將調用構造器,該構造器為`num`浮點數據分配內存。 當對象被銷毀時,即,對象超出范圍時,將自動調用析構器。 ```cpp ~Test() { delete[] ptr; } ``` 該析構器執行`delete[] ptr;`,并將內存返回給操作系統。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看