memory used by vector<int> vs vector<char>
Hi, 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? or which is more efficient? Thanks, Lloyd
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.
Thanks Igor, that clarifies my question
On Wed, Aug 21, 2013 at 5:13 PM, Igor R
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 mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On 8/21/13 7:33 AM, Lloyd wrote:
Hi,
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? or which is more efficient?
Thanks, Lloyd
Since on most machines, an int is bigger than a char, the vector<int> will allocate more memory than a vector<char>. Note that the memory for the elements will NOT show up in the sizeof() the vector, as the vector object itself doesn't hold the elements, but just a control block, the elements are normally stored on the heap. As to efficiency, if you are only storing chars, than the vector<char> is probably more efficient, if you are actually storing something bigger (like an int), than a vector<char> is probably the wrong thing to be using in the first place. -- Richard Damon
participants (3)
-
Igor R
-
Lloyd
-
Richard Damon