4

The vector<bool> class in the C++ STL is optimized for memory to allocate one bit per bool stored, rather than one byte. Every time I output sizeof(x) for vector<bool> x, the result is 40 bytes creating the vector structure. sizeof(x.at(0)) always returns 16 bytes, which must be the allocated memory for many bool values, not just the one at position zero. How many elements do the 16 bytes cover? 128 exactly? What if my vector has more or less elements?

I would like to measure the size of the vector and all of its contents. How would I do that accurately? Is there a C++ library available for viewing allocated memory per variable?

  • 3
    You are confusing *the size of an object* with *the memory allocated by an object*. – Drew Dormann Jul 20 '15 at 17:54
  • I understand the difference, I just don't know what to use to find memory allocated –  Jul 20 '15 at 17:57
  • 1
    That's a difficult concept to define, since a byte of memory may not have well-defined ownership. It could "belong to" several variables, or it could remain *allocated* after its "owning variable" is destroyed. – Drew Dormann Jul 20 '15 at 18:00

1 Answers1

4

I don't think there's any standard way to do this. The only information a vector<bool> implementation gives you about how it works is the reference member type, but there's no reason to assume that this has any congruence with how the data are actually stored internally; it's just that you get a reference back when you dereference an iterator into the container.

So you've got the size of the container itself, and that's fine, but to get the amount of memory taken up by the data, you're going to have to inspect your implementation's standard library source code and derive a solution from that. Though, honestly, this seems like a strange thing to want in the first place.

Actually, using vector<bool> is kind of a strange thing to want in the first place. All of the above is essentially why its use is frowned upon nowadays: it's almost entirely incompatible with conventions set by other standard containers… or even those set by other vector specialisations.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • There's also the whole notion of [capacity vs size](https://stackoverflow.com/questions/6296945/size-vs-capacity-of-a-vector) of a `std::vector` as it may be allocating more memory than it needs (at the exact moment) for performance reasons. – Cory Kramer Jul 20 '15 at 17:57
  • `sizeof(x) + sizeof(decltype(x)::value_type)*x.capacity()` was what I was looking for. That helped. –  Jul 20 '15 at 17:58
  • @CoryKramer: True, though the only use cases I can vaguely come up with for this question are for analysing how much memory is used by the container, regardless of whether all of it is presently used for actually storing something. – Lightness Races in Orbit Jul 20 '15 at 17:58
  • 2
    @apoorvk: But it's _very_ wrong. `decltype(x)::value_type` is `bool` here, which is by definition not what you're going to be storing in practice (otherwise what's the point of it all?) – Lightness Races in Orbit Jul 20 '15 at 17:59
  • Right, so that calculation gives an inaccurate result, because there is more memory allocated at a time than just the single bit per boolean in the contents? –  Jul 20 '15 at 18:02
  • 1
    @apoorvk: It gives an inaccurate result because it pretends that each boolean takes 1 byte. And you don't even _know_ that each boolean will instead take 1 bit, precisely. You just don't know anything at all, in general. You will have to examine your specific implementation, as I said. – Lightness Races in Orbit Jul 20 '15 at 18:04