Article Directory
I look at the dark horse code, there is such a piece of code in the MyArray class:
// ...
// 重载= 操作符 防止浅拷贝问题
MyArray &operator=(const MyArray &myarray)
{
if (this->pAddress != NULL)
{
delete[] this->pAddress;
this->m_Capacity = 0;
this->m_Size = 0;
}
this->m_Capacity = myarray.m_Capacity;
this->m_Size = myarray.m_Size;
this->pAddress = new T[this->m_Capacity];
for (int i = 0; i < this->m_Size; i++)
{
this->pAddress[i] = myarray[i];
}
return *this;
}
// ...
there is a sentencedelete[] this->pAddress;
Here
delete[] this->pAddress;
isthis->pAddress
the statement used to release the memory space of the dynamic array pointed to by .delete[]
It is an operator used in C++
to release the dynamically allocated array memory space. It will call the destructor of the array elements and release the memory space occupied by the array. In this code,this->pAddress
it points to a dynamically allocated array, and the problem of memory leak is avoided bydelete[] this->pAddress;
releasing the memory space occupied by the array.
But I have a question, this->pAddress
it is the address of the first element of the array, delete[]
how do I know the length of the array and how much needs to be released?
this->pAddress points to the address of the first element of the dynamically allocated array, not the address of the entire array. In this code, we use the delete[] operator to release the dynamically allocated array memory space, which automatically calculates the size of the array and releases the memory space occupied by the entire array. Therefore, even if this->pAddress points to the address of the first element of the array, using delete[] this->pAddress; can correctly release the memory space occupied by the entire array.
How does it automatically calculate the size of the array?
In C++, when using
new
the operator to dynamically allocate array memory space, the compiler will store an additional integer to record the size of the array when allocating memory space. This integer is usually called "array length", and it is stored at the front of the array memory space, occupying 4 bytes (32-bit system) or 8 bytes (64-bit system) of space. When we usedelete[]
the operator to release the dynamically allocated array memory space, the compiler will automatically read the length of the array, calculate the size of the memory space occupied by the entire array according to the length of the array, and then release the memory space. Therefore, we do not need to manually calculate the size of the array, nor do we need to manually release the memory space of the array, the compiler will automatically complete these tasks.
Can ordinary pointers be released with delete[] even if they are not arrays?
Can't.
delete[]
The operator can only be used to release the memory space of an array dynamically allocated usingnew[]
the operator , and cannot be used to releasenew
the memory space of a single object dynamically allocated using the operator, nor can it be used to release the memory space pointed to by an ordinary pointer. If we usedelete[]
the operator to release non-array memory space, or usedelete
the operator to release thenew[]
array , it will lead to undefined behavior, which may cause problems such as program crashes or memory leaks. Therefore, when usingdelete
ordelete[]
operators, we need to ensure that the type of memory space they release matches the type of memory space allocated by their corresponding operators.