# new 運算子與 delete 運算子

int *ptr = new int;

int *ptr = new int(100);

``#include <iostream> using namespace std; int main() {    int *ptr = new int(100);     cout << "空間位置:" << ptr          << endl;     cout << "空間儲存值：" << *ptr         << endl;     *ptr = 200;     cout << "空間位置:" << ptr          << endl;    cout << "空間儲存值：" << *ptr          << endl;    delete ptr;     return 0; }``

int *arr = new int[1000];

delete [] arr;

``#include <iostream> using namespace std; int main() {    int size = 0;     cout << "請輸入陣列長度：";     cin >> size;     int *arr = new int[size];     cout << "指定元素值：" << endl;     for(int i = 0; i < size; i++) {         cout << "arr[" << i << "] = ";         cin >> *(arr+i);     }     cout << "顯示元素值：" << endl;     for(int i = 0; i < size; i++) {        cout << "arr[" << i << "] = " << *(arr+i)             << endl;     }     delete [] arr;      return 0; }``

arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5

arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5

``#include <iostream> using namespace std; int main() {    int m = 0;    int n = 0;     cout << "輸入二維陣列維度：";     cin >> m >> n;     int *ptr = new int[m*n];      for(int i = 0; i < m; i++) {        for(int j = 0; j < n; j++) {            *(ptr + n*i + j) = i+j;         }    }    for(int i = 0; i < m; i++) {         for(int j = 0; j < n; j++) {            cout << *(ptr+n*i+j) << "\t";         }         cout << endl;     }     delete [] ptr;      return 0; }``

0       1       2       3       4
1       2       3       4       5
2       3       4       5       6
3       4       5       6       7