sizeof()std :: vector(C ++) [英] sizeof() std::vector (C++)

查看:118
本文介绍了sizeof()std :: vector(C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个主题已经有一个主题,但我仍然怀疑。要计算向量的大小,哪个正确:

There is a topic already on this topic but I have doubts still. To calculate the size of a vector, which one is correct:

sizeof(VEC) + sizeof(int) * VEC.capacity()

VEC.capacity() * (sizeof(VEC) + sizeof(int))


推荐答案

向量的大小是什么意思?向量对象的大小只是

What do you mean by size of the vector? The size of the vector object is just

sizeof(vec);

如果您对向量在堆上分配了多少内存感兴趣,可以使用



If you are interested in how much memory the vector has allocated on the heap, you can use

vec.capacity()*sizeof(T)

所以,如果你添加这些,你会得到多少内存你已经失去因为向量。

So, if you add these, you'll get how much memory you've "lost" because of the vector.

vec.capacity()*sizeof(T) + sizeof(vec)


$ b b

请注意,确切分配多少内存是依赖于实现的。这只是上面的公式将在大多数(如果不是所有实现)上实际上实际上正确(或大致正确)。

Please note that exactly how much memory is allocated is implementation-dependent. It's just that the formula above will be practically correct (or approximately correct) on most if not all implementations.

这篇关于sizeof()std :: vector(C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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