Skip to content

Latest commit

 

History

History
34 lines (28 loc) · 617 Bytes

211107.md

File metadata and controls

34 lines (28 loc) · 617 Bytes

c++ new 与 delete

可以在堆区开辟空间

  1. new 可以在堆区开辟空间
  2. delete 可以释放开辟的空间
#include <iostream>
#include <string>
using namespace std;

int main () {
    int * a = new int(10);
    float * b = new float(10.2);
    string * s = new string("abc");
    int * c = new int[2];
    c[0] = 1;
    c[1] = 2;

    cout << *a << endl;
    cout << *b << endl;
    cout << *s << endl;
    cout << c[0] << endl;
    cout << c[1] << endl;

    delete a;
    delete b;
    delete[] c;
    delete s;
    cout << *a << endl;
    return 0;
}