Boost logo

Boost Users :

Subject: Re: [Boost-users] memory used by vector<int> vs vector<char>
From: Igor R (boost.lists_at_[hidden])
Date: 2013-08-21 07:43:55


> If I create a vector<int> of 100 elements and a vector<char> of 100
> elements, will there be any difference in the total memory used by them?

Yes, if sizeof(int)>sizeof(char) on your platform, then vector<int>
consumes more memory than vector<char>, because the Standard requires
that "the elements of a vector are stored contiguously, meaning that
if v is a vector where T is some type other than bool, then it obeys
the identity &v[n] == &v[0] + n for all 0 <= n < v.size()."

> or which is more efficient?

What do you mean by saying "more efficient"?
vector<char> consumes less memory, but accessing its elements might be
less efficient on some platforms.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net