Freeing memory when deleting an array in C ++

In C ++, when we assign an array, there is no way to find out the size of the array after its initialization. Then, as the delete operator, it knows the amount of memory that needs to be deleted when I try to free memory at the end of my program.

int main()
{
    int* p = new int[10];
    int* q = new int[30];
    //... bunch of code
    //...
    // ... bunch of code
    delete[] p;
    delete[] q;
    return 0;
}
+4
source share
1 answer

The statement newcompletes the creation of the record on the heap, and the heap distributor knows how to de-distribute the things that he previously allocated. This information is usually not available for your code, because all C ++ internal elements that you should not interact with.

So basically heap metadata describes this distribution.

, ++ , new delete[] , . , std::allocator, , -, , , - . , .

+12

Source: https://habr.com/ru/post/1659663/


All Articles