I have a data structure that is an std::vector of structs, where each struct also contains an std::vector. I want to calculate in advance an upper limit to the memory needed to represent the entire structure. TO do this, in testing I want to calculate the memory requirements for a finalized structure and compare it to my estimation.
To do this I use the following code:
struct SequenceInfo {
unsigned long num1;
unsigned long num2;
unsigned long vectorLength;
std::vector<unsigned long> values;
};
// A vector of sequence data to represent all data
typedef std::vector<SequenceInfo> SequenceInfoVec;
void foo(SequenceInfoVec& vec)
{
getVec(vec);
std::size_t actualSize = sizeof(SequenceInfoVec);
for (SequenceInfoVec::iterator it1 = vec.begin(); it1 != vec.end(); ++it1)
{
actualSize += sizeof(SequenceInfo) +
sizeof((*it1).values[0]) * (*it1).values.size();
}
cout << "memory size of vec is: " << actualSize << endl;
}
Is this the correct way to calculate the memory requirements of the data structure, (disregarding small OS overhead for memory allocation)?