memory consumed by a string vector in c++ -


i creating vector of strings in c++. need total memory consumed in bytes vector.

since strings of variable size, right iterating through every vector element , finding length @ end multiplying size of char. need cleaner solution.

vector<string> v; //insertion of elements int len=0; for(int i=0;i<v.size();i++)                     len+=v[i].length(); int memory=sizeof(char)*len; 

alternatively solution find memory consumption of string array do. let's

string a[size] 

find number of bytes a?

a rough estimate of memory occupied std::vector<string>:

      sizeof(std::vector<string>) // size of vector basics.       + sizeof(std::string) * vector::size()  // size of string object, not text //  1 string object each item in vector. //  **the multiplier may want capacity of vector,  //  **the reserved quantity.       + sum of each string's length; 

since vector , string not fixed sized objects, there may additional overhead occupied dynamic memory allocations. why estimate.

edit 1:
above calculation assumes single character unit; not multibyte characters.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -