简体   繁体   中英

C++ Memory leaks vectors(?)

In C++, the importance of deallocating memory when the program is either exiting or no longer serves a purpose is important. So if this is allocation of a dynamic array

char** dynamicArr = nullptr;

for (int i = 0; i < x; i++) {
    mapPtr[i] = new char[y];
}

and this is deallocation of a dynamic array

for (int i = 0; i < x; i++) {
        delete[] mapPtr[i];
}

delete[] mapPtr;

However, when it comes to vectors, I noticed that my global vector with 0 elements inside seems to be causing some memory leaks.

I've read up on this link , a user commented that

No. The std::vector will automatically de-allocate the memory it uses

Screenshot of my debugging.

可能的向量内存泄漏

I have also tried these steps to clear the vector as well as make sure the vector inside the struct citySummInfo has shrunk to fit and clear hopefully not getting any memory leak but to no avail. Is there any way that I'm doing it wrong?

As what @JohnFilleau have mentioned

_CrtDumpMemoryLeaks() should be called at the point in the program where you want to see what is remaining on the heap. Since your vectors are statically allocated, they will not have been destroyed at the time you call this function.

_CrtDumpMemoryLeaks() is meant to place right before the program terminates, and since my vectors are statically allocated, it has not been deallocated at the time when _CrtDumpMemoryLeaks() has been called hence the "leaks".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM