C ++ Study Notes (15th): How the vector object grows in the memory space, learning notes vector

Source: Internet
Author: User

C ++ Study Notes (15th): How the vector object grows in the memory space, learning notes vector

How does a vector object increase in memory?

 

We all know that vector objects are dynamically stored. From this point of view, they are a bit like linked lists, which can dynamically increase or decrease elements. We also know that the linked list contains pointer variables, which are used to store the addresses of the previous and next elements. It is precisely because of the existence of these two pointers that we can achieve dynamic data storage, that is, we do not have to apply for a good space as an array. The disadvantage of a linked list is that it cannot quickly access the elements at random, and it must be searched by pointer layer by layer.

However, vector can store data dynamically and supports fast Random Access (using subscript or pointer to access elements ). For data types that can be searched by subscript, the storage method must be continuous, that is, each element is stored next to the previous element.

In this case, a problem occurs for the vector. How much memory space should I apply for when I first define the vector? If the application is small, where can I store new data? If the application is too large and I can't use it, isn't it a waste of memory space?

Of course, for the standard library type of vector, we usually only care about how to use it, rather than how to implement it. However, it is better to know how it is implemented in the bucket.


We know that elements in the container are continuously stored and the container size is variable. What will happen if we consider adding elements to the vector? If there is no space to accommodate new elements, the container cannot simply add them to other locations in the memory, because the elements of the vector must be stored continuously. Therefore, the container must allocate a new space to save the used elements and new elements. Move existing elements from the old location to the new space, add new elements, and release the old space. If we execute this operation every time we add a new element, the performance will obviously slow to unacceptable.


To avoid the above cost, the standard library adopts a policy that can reduce the container Space reallocation. When you have to obtain a new memory space, the implementation of the vector will usually allocate more memory space than the new space. Containers reserve these spaces as backups to save more new elements. In this way, you do not need to re-allocate the container memory space every time you add new elements.

 

The vector type provides some member functions that allow us to interact with memory allocation in reality.

C. capacity (): How many elements can c save without re-allocating memory space?

C. reserve (n) allocate memory space that can accommodate at least n elements

C. shrink_to_fit () reduces capacity () to the same size as size. Size () indicates the number of elements saved in the vector.

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.