Getting the size, in memory of a vector

Hello,

I'm trying to create a function that estimates the memory that a vector will occupy in memory when run (windows 7 64-bit).

Initially I thought the best way of doing this would be to use the capacity() operator, but this seems to return rubbish.

I have a vector of structures, with each structure containing 2 vectors and a single-byte char value. Here is what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
float size_estimation(vector<struct_mystruct> MYSTRUCT) {
    unsigned short MYSTRUCT_length = MYSTRUCT.size();

    float size_estimation;
    for (unsigned short index = 1; index != MYSTRUCT_length; index++) {
        size_estimation += MYSTRUCT.at(index).vec1.size()*2; // type 'short'
        size_estimation += MYSTRUCT.at(index).vec2.size()*4; // type 'float'
        size_estimation += 1; //integer of type 'char'
    }
    size_estimation = size_estimation / 1024 / 1024; //show result in MB
    return size_estimation;
}


I then call this function and compare the result to the result in my task manager while running the executable. For small sizes of MYSTRUCT the estimation is quite bad - to be expected given all the includes, etc. that go into running an executable. However, for larger values there seems to be a lingering error, suggesting that I am missing something:

For a MYSTRUCT.size() of 2000, and for vec1.size()/vec2.size() of 100 each, the predicted size is around 382 MB, while the space occupied in the task manager is 395 MB.

The difference of around 13 MB is large compared to the <1MB size of the program when MYSTRUCT is left out alltogether.

I am guessing that some room is required to define the length of each vector, and some sort of wrapper for each structure, how can I get these values?? Is there anything else I should be considering?
What?
1
2
std::vector<T> v(n);
size_t size=v.capacity()*sizeof(T)+sizeof(v);
Sorry, but I'm really new to c++, can you explain what T/v/n are? Or just replace them with the names from my example then I should be able to figure it out... Thanks for you help though!
T = type of element contained in the vector
v = the vector itself
n = number of vector elements
Topic archived. No new replies allowed.