Use reserve to avoid unnecessary allocations again

Source: Internet
Author: User



The great thing about STL containers is that they will grow on their own initiative to accommodate the data you put in, just without exceeding their maximum limit. For vectors and strings, the growth process is accomplished by invoking operations similar to realloc whenever a lot of other space is required.

This realloc -like operation is divided into 4 parts:

  1. Allocates a piece of new memory that is a multiple of the current capacity.

    In most implementations, the capacity of thevector and string increases each time in multiples of 2 , that is, each time the container needs to be expanded, their capacity is doubled.

  2. Copy all the elements of the container from the old memory to the new memory.

  3. Destruction of objects in old memory.

  4. Frees old memory.

    The reserve member function allows you to minimize the number of times you allocate again, thus avoiding the overhead of allocating memory and pointer / iterator / reference failures again. Before explaining How reserve can do that. I'll briefly summarize the 4 member functions that are interrelated but sometimes confused. In a standard container, only the vector and string provide all 4 of these functions:

    size () shows how many elements are in a container. It does not tell you how much memory the container allocates for the elements it includes.

    capacity () shows how many elements can be accommodated by a container using the allocated memory.

    This is the total number of elements that the container can hold. Instead of how many elements it can hold.

    Suppose you want to know how much of a vector is used memory, you have to subtract size ()from Capacity ( ). Assume that size and capacity return the same value. It means there is no more space left in the container.

    Resize (container::size_type N) forces the container to change to a state that includes n elements.

    After calling resize ,size returns n.

    Assuming that N is smaller than the current size, the elements at the end of the container will be refactored out. Assume that N is larger than the current size. The new element created by the default constructor is added to the end of the container. Assuming that N is larger than the current capacity, memory will be allocated again before the element is added.

    Reserve (container::size_type N) forces the container to change its capacity to at least N. The premise is that n is not less than the current size. This typically results in another allocation, since the capacity is added. (assuming that n is smaller than the current capacity, the vector ignores the call.) Do nothing, while a string may reduce its capacity to the maximum of size () and n , but the size of the string must remain the same. )

    For example, suppose you want to create a vector<int>that includes a value between 1 and.

    Assume that the reserve is not used. You might do this:

    Vector<int> v;

    for (int i = 1;i <= 1000;++i)

    V.push_back (i);

    The loop will cause 2 to ten times to be allocated in the process. Assume that you use the reserve, as seen in the following:

    Vector<int> v;

    V.reserve (1000);

    for (int i = 1;i <= 1000;++i)

    V.push_back (i);

    No more allocations will occur during the loop.

Suppose you want to remove the excess capacity. See how to effectively remove excess capacity

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Use reserve to avoid unnecessary allocations again

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.