When you use new, two things happen.
First, the memory is allocated (through the function "operator new ).
Second, one (or more) constructor will be called for this memory. When you use Delete, two things happen: one or more destructor are called for the memory, and the memory is released.
The memory used by the array usually includes the "array size" record so that delete can know how many destructor are called.
When you call New, use []. You must also use [] when calling Delete. If you do not use [] when calling New, you should not use [] when calling Delete.
When the class you write contains a dynamic memory allocation pointing to a pointer and provides multiple constructors, you must be careful to use the same form
New initializes pointer members. Otherwise, you do not know the form of Delete in the destructor. Because there is only one destructor.
This rule is also important for people who rarely use typedef:
Typedef STD: String addresslines [4];
Since addresslines is an array, if new is used as follows:
STD: string * pal = new addresslines; // note that "New addresslines" returns a string *, just like "New String [4 ]".
It must match the delete in the array format:
Delete pal; // The behavior is undefined.
Delete [] pal; // very good.
To avoid such errors, we recommend that you do not perform typedef actions on the array format. This is easy to achieve, because the C ++ standard library contains templates such as string and vector, the array requirement can be reduced to almost 0. You can define addresslines in this example as vector <string>.