Memory Allocation and release of vector in C ++ STL

Source: Internet
Author: User

1. vector memory growth one of the features of vector: memory space will only grow, will not decrease, quoted C ++ Primer: To support fast random access, vector container elements are stored in a continuous manner, each element is stored next to the previous element. Imagine that when a vector adds an element, In order to store this feature continuously, it needs to re-allocate space, copy elements, and cancel the old space, which is unacceptable. Therefore, when the STL implementer allocates memory for the vector, it actually allocates more capacity than the current space. That is to say, the vector container reserves some additional storage areas for storing newly added elements, so that it does not have to re-allocate the memory space of the entire container for each new element. When push_back is called, every time the push_back operation is executed, it is equivalent to allocating the size of the underlying array implementation; this implementation is embodied in the vector implementation, that is, every time push_back is an element, you need to re-allocate the storage of a large element, then copy the original element to the new storage, and then copy the elements of push_back, finally, we need to analyze the original vector and release the original memory. For example, the following program: # include <iostream> # include <cstdlib> # include <vector> using namespace std; class Point {public: Point () {cout <"construction" <endl;} Point (const Point & p) {cout <"copy construction" <endl ;}~ Point () {cout <"destruction" <endl ;}; int main () {vector <Point> pointVec; Point a; Point B; pointVec. push_back (a); pointVec. push_back (B); cout <pointVec. size () <std: endl; return 0;} Where pointVec is executed. push_back (a); at this time, the vector will apply for a memory space, and call the copy constructor to put a in the vector and then call pointVec. push_back (B); at this time, the memory is not enough to expand, and the memory needs to be re-allocated. Then, the copy constructor is called to copy a to the new memory, and then B to the new memory, at the same time, someone calls the Point copy constructor and finally releases the original memory. At this time, the Point destructor is called. 2. the memory of the vector is released because the space occupied by the vector memory only increases and does not decrease. For example, if you allocate 10,000 bytes first, then erase drops the last 9,999 bytes and leaves a valid element, however, the memory usage is still 10,000. All memory space can be recycled by the system only when the vector destructor is created. Empty () is used to check whether the container is empty. clear () can clear all elements. However, even if clear () is used, the memory space occupied by the vector remains the same, and memory recovery cannot be guaranteed. Deque can be used to dynamically reduce the space. If the vector is used, you can use swap () to release the memory. The specific method is as follows: vector <Point> (). swap (pointVec); // or pointVec. swap (vector <Point> () standard template: template <class T> void ClearVector (vector <T> & vt) {vector <T> vtTemp; veTemp. swap (vt);} swap () is an exchange function that leaves the vector from its own scope and forcibly releases the memory space occupied by the vector, the simplest way to release vector memory is vector <Point> (). swap (pointVec ). If pointVec is a member of a class, the vector <Point> () cannot be used (). swap (pointVec) is written into the class destructor. Otherwise, the double free or uption (fasttop) error may occur because the memory is released repeatedly. (The previous pointVec. swap (vector <Point> () fails to be compiled using G ++.) 3. in other cases, the memory is released. If the vector stores pointers, the objects pointed to by these pointers will not be destroyed and the memory will not be released. In the following example, the object pointer dynamically applied by the new operation when elements in the vector are: # include <vector> using namespace std; vector <void *> v; call v. push_back () This pointer is used to release the memory when the program exits or as needed: for (vector <void * >:: iterator it = v. begin (); it! = V. end (); it ++) if (NULL! = * It) {delete * it; * it = NULL;} v. clear ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.