C++基础之new&delete

    科技2024-08-11  29

        Dynamic memory allocation

        ◆ new

            ★ new int;

            ★ new Stash;

            ★ new int[10];

        new要做两件事情:① 分配空间;② 在分配完之后,如果分配的是一个类,那么会调用该类的构造函数来构造一个对象,new运算符的返回值就是地址

        ◆ delete

            ★ delete p;

            ★ delete[] p;

        new and delete

        ◆ new is the way to allocate memory as a program runs. Pointers become the only access to that memory.

        ◆ delete enables you to return memory to the memory pool when you are finished with it.

        Dynamic Arrays

    int* psome = new int[10];

        ◆ The new operator returns the address of the first element of the block.

    delete[] psome;

        ◆ The presence of the brackets tells the program that it should free the whole array, not just the element

        如果没有写“[]”,情况就是不一样的:空间还是会被回收,但是析构只有第一个会被调用

        The new-delete mech

    int* p = new int; int* a = new int[10]; Student* q = new Student(); Student* r = new Student[10]; delete p; a++; delete[] a; //a++;就会找不到之前分配的空间,报错 delete q; //知道指针q的类型,根据类型调用析构函数 delete r; //不带“[]”,没有告诉编译器有多个对象,只会调用r所指的那个对象的析构函数一次,之后的9个就没管,但是空间全部被回收 delete[] r; //带“[]”,告诉编译器是数组有多个对象,调用10次析构函数,然后再回收空间

    【例】

    #include <iostream> using namespace std; class A { private: int i; public: void f(); void set(int i); A(); ~A(); }; void A::f() { cout << "hello" << endl; } void A::set(int i) { this->i = i; //就近原则,成员变量i会被覆盖,使用this->i解决 } A::A() { i = 0; cout << "A::A()" << endl; } A::~A() { cout << "A::~A(), i = " << i << endl; } int main(int argc, const char* argv[]) { A* p = new A[10]; for (int i = 0; i < 10; i++) { p[i].set(i); } delete p; return 0; }

        程序的运行结果说明,如果只写delete p;  那么析构函数A::~A();  只被调用了一次:

        如果将程序改为:

    #include <iostream> using namespace std; class A { private: int i; public: void f(); void set(int i); A(); ~A(); }; void A::f() { cout << "hello" << endl; } void A::set(int i) { this->i = i; //就近原则,成员变量i会被覆盖,使用this->i解决 } A::A() { i = 0; cout << "A::A()" << endl; } A::~A() { cout << "A::~A(), i = " << i << endl; } int main(int argc, const char* argv[]) { A* p = new A[10]; for (int i = 0; i < 10; i++) { p[i].set(i); } delete[] p; return 0; }

        我们可以看到,类A的析构函数A::~A();  被调用了10次:

        Tips for new and delete

        ◆ Don't use delete to free memory that new didn't allocate

        ◆ Don't use delete to free the same block of memory twice in succession.

        ◆ Use delete[] if you used new[] to allocate an array.

        ◆ Use delete (no brackets) if you used new to allocate a single entity.

        ◆ It's safe to apply delete to the null pointer (nothing happens).

    #include <iostream> using namespace std; int main(int argc, const char* argv[]) { int* p = 0; delete p; return 0; }

        delete空指针的应用:

    #include <iostream> using namespace std; class A { private: int i; int* p; public: A(); ~A(); void f(); }; A::A() { p = 0; } A::~A() { delete p; } void A::f() { p = new int; } int main(int argc, const char* argv[]) { return 0; }

     

    Processed: 0.009, SQL: 8