Vector Memory Allocation

Source: Internet
Author: User
    1. /* Conclusion:
    2. The existence of vector frees developers from having to worry about memory application and release. However, the memory allocated behind the vector is allocated in multiples of 2. When the current capacity is insufficient for the inserted element, allocate a new memory, which is twice the size of the original vector capacity, and then copy the old memory to release the old memory, copying constructor and destructor may be involved multiple times, which is also the disadvantage of vector. */

When using the cocos2d-x engine, the program will collapse when the cclayer, ccsprite destructor are called multiple times.
  1. # Include <iostream>
  2. # Include <vector>
  3. Using namespace STD;
  4. Int main ()
  5. {
  6. Vector <int> ivec;
  7. Cout <"Size:" <ivec. Size () <Endl;
  8. Cout <"capacity" <ivec. Capacity () <Endl; // 1 element, container capacity is 1
  9. Ivec. push_back (1 );
  10. Cout <"Size:" <ivec. Size () <Endl;
  11. Cout <"capacity" <ivec. Capacity () <Endl; // two elements, container capacity 2
  12. Ivec. push_back (2 );
  13. Cout <"Size:" <ivec. Size () <Endl;
  14. Cout <"capacity" <ivec. Capacity () <Endl; // The container capacity is 4
  15. Ivec. push_back (3 );
  16. Cout <"Size:" <ivec. Size () <Endl;
  17. Cout <"capacity" <ivec. Capacity () <Endl; // four elements, container capacity: 4
  18. Ivec. push_back (4 );
  19. Ivec. push_back (5 );
  20. Cout <"Size:" <ivec. Size () <Endl;
  21. Cout <"capacity" <ivec. Capacity () <Endl; // five elements, with a container capacity of 8
  22. Ivec. push_back (6 );
  23. Cout <"Size:" <ivec. Size () <Endl;
  24. Cout <"capacity" <ivec. Capacity () <Endl; // six elements, with a container capacity of 8
  25. Ivec. push_back (7 );
  26. Cout <"Size:" <ivec. Size () <Endl;
  27. Cout <"capacity" <ivec. Capacity () <Endl; // seven elements, container capacity 8
  28. Ivec. push_back (8 );
  29. Cout <"Size:" <ivec. Size () <Endl;
  30. Cout <"capacity" <ivec. Capacity () <Endl; // 8 elements, container capacity is 8
  31. Ivec. push_back (9 );
  32. Cout <"Size:" <ivec. Size () <Endl;
  33. Cout <"capacity" <ivec. Capacity () <Endl; // nine elements, container capacity: 16
  34. /* Conclusion:
  35. The existence of vector frees developers from having to worry about memory application and release. However, the memory allocated behind the vector is allocated in multiples of 2. When the current capacity is insufficient for the inserted element, allocate a new memory, which is twice the size of the original vector capacity, and then copy the old memory to release the old memory, copying constructor and destructor may be involved multiple times, which is also the disadvantage of vector. */
  36. Return 0;
  37. }

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.