空矢量的开销成本是多少? [英] What is the overhead cost of an empty vector?

查看:86
本文介绍了空矢量的开销成本是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有空向量vs具有指向向量的指针的内存开销是多少?



选项A:

  std :: vector< int> v; 

选项B:

  std :: vector< int> * v = NULL; 

我相信选项B需要1个32位指针(假设这里为32位)

解决方案

对于问题:这取决于实现。对于MSVC 7.1,这是:

  std :: cout< sizeof(std :: vector< int>)< std :: endl; 

给我16.(3指针:容量的开始,结束和结束,加上一个分配器)



但是应注意指向矢量的指针有较大的开销:


$ b $


  • >

    What is the memory overhead of having an empty vector vs having a pointer to a vector?

    Option A:

    std::vector<int> v;
    

    Option B:

    std::vector<int> *v = NULL;
    

    I believe that option B takes 1 32 bit pointer (assuming 32 bit here) How much memory does the empty 'v' take up?

    解决方案

    As for the question as asked: It depends on the implementation. With MSVC 7.1 this:

    std:: cout << sizeof(std::vector<int>) << std::endl;
    

    gives me 16. (3 pointers: begin, end, and end of capacity, plus an allocator)

    However it should be noted that the pointer-to-vector has a larger overhead:

    • in both time and space in the non-empty case
    • in complexity in all cases.

    这篇关于空矢量的开销成本是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆