C++ vector内存分配机制

C++ vector其实就相当于动态数组,它的内存会随着size的增加而不断的增长。
vector有两个函数,一个是capacity(),返回对象缓冲区(vector维护的内存空间)实际申请的空间大小,另一个size(),返回当前对象缓冲区存储数据的个数。对于vector来说,capacity是永远大于等于size的,档capacity和size相等时,vector就会扩容,capacity变大。

比如vector最常用的push_back操作,它的整个过程是怎么一个机制呢?
在调用push_back时,若当前容量(capacity)已经不能够容纳新的元素,即此时capacity=size,那么vector会重新申请一块内存,把之前的内存里的元素拷贝到新的内存当中,然后把push_back的元素拷贝到新的内存中,最后要析构原有的vector并释放原有的内存。这个过程的效率是极低的,为了避免频繁的分配内存,C++每次申请内存一般都会成倍的增长,例如之前是4,那么重新申请后就是8,以此类推。

下面通过一个实例来验证整个过程:
#include <vector>  
#include <iostream>  
using namespace std;  

class Test  
{  
public:  
    Test(char c): m(c)  
    {  
        cout << "construction" << " " << m << endl;  
    }  
    Test(const Test& p): m(p.m)  
    {  
        cout << "copy construction" << " " << p.m << endl;  
    }  
    ~Test()  
    {  
        cout << "destruction" << " " << m << endl;  
    }
    int m;  
};  

int main()  
{  
    Test test[10] = {0,1,2,3,4,5,6,7,8,9};  
    cout << "**************************" << endl;  
    vector<Test> arr;  
    for (int i = 0; i < 10; i++)  
    { 
	cout << "~ ~ ~ ~ ~ ~" << endl; 
        arr.push_back(test[i]);  
        cout << "capacity=" << arr.capacity() << ",size=" << arr.size() << endl;  
        cout << "--------------------------" << endl;  
    }
    
    //
    test[0].m = 100;  

    cout << "...end..." << endl; 
 
}  

运行结果:
construction 0
construction 1
construction 2
construction 3
construction 4
construction 5
construction 6
construction 7
construction 8
construction 9
**************************
~ ~ ~ ~ ~ ~
copy construction 0
capacity=1,size=1
--------------------------
~ ~ ~ ~ ~ ~
copy construction 1
copy construction 0
destruction 0
capacity=2,size=2
--------------------------
~ ~ ~ ~ ~ ~
copy construction 2
copy construction 0
copy construction 1
destruction 0
destruction 1
capacity=4,size=3
--------------------------
~ ~ ~ ~ ~ ~
copy construction 3
capacity=4,size=4
--------------------------
~ ~ ~ ~ ~ ~
copy construction 4
copy construction 0
copy construction 1
copy construction 2
copy construction 3
destruction 0
destruction 1
destruction 2
destruction 3
capacity=8,size=5
--------------------------
~ ~ ~ ~ ~ ~
copy construction 5
capacity=8,size=6
--------------------------
~ ~ ~ ~ ~ ~
copy construction 6
capacity=8,size=7
--------------------------
~ ~ ~ ~ ~ ~
copy construction 7
capacity=8,size=8
--------------------------
~ ~ ~ ~ ~ ~
copy construction 8
copy construction 0
copy construction 1
copy construction 2
copy construction 3
copy construction 4
copy construction 5
copy construction 6
copy construction 7
destruction 0
destruction 1
destruction 2
destruction 3
destruction 4
destruction 5
destruction 6
destruction 7
capacity=16,size=9
--------------------------
~ ~ ~ ~ ~ ~
copy construction 9
capacity=16,size=10
--------------------------
...end...
destruction 0
destruction 1
destruction 2
destruction 3
destruction 4
destruction 5
destruction 6
destruction 7
destruction 8
destruction 9
destruction 9
destruction 8
destruction 7
destruction 6
destruction 5
destruction 4
destruction 3
destruction 2
destruction 1
destruction 100



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值