1

I need calculate memory (static and dynamic) taken by the vector myVec; I have calculated the size in following manner

size = sizeof(myVec) * myVec.size();

I need to know the what I did is correct or not ?

struct S1
{
  vector<int> v1;
  IplImage* image;
  vector<CvRect> rect;
};

struct S2
{
  vector<S1> var1;
  vector<int>  var2;
  IplImage* img1;
};

vector<S2> myVec;


//size = sizeof(myVec) * myVec.size(); ?????
5
  • Static or dynamic memory?
    – awesoon
    Nov 25, 2014 at 6:52
  • Won't sizeof(myVec) be enough as it get the size of the elements in it too.... ???
    – Xlander
    Nov 25, 2014 at 6:54
  • myVec.count() give total number of element in the vector... then sizeof(myVec.at(0)) give size taken by strut now {myVec.count()*sizeof(myVec.at(0))} give total size caten by vector
    – Singhak
    Nov 25, 2014 at 6:58
  • I don't think you can get the real dynamic memory usage because of polymorphism (at least). Maybe, not this way.
    – awesoon
    Nov 25, 2014 at 7:14
  • Use a tool like the memory profiler from gperftools to show you who's allocating, where and how much. Alternatively, write a custom counting allocator and use it for all the types that need to be measured.
    – Sam
    Nov 25, 2014 at 8:36

2 Answers 2

2

You cannot easily determine both the static and dynamic size of a container in C++, because each contained instance can allocate its own internal memory (as @enhzflep pointed out in comments).

However, if you really need to do that, and if you know what types you might want to store in your container, you might use templates to assemble the final algorithm for you. I.e., something along the lines of:

template<typename T>
struct compute_size {
    static unsigned of(const T& t) {
        assert(false && "not implemented");
        return 0;
    }
};

template<typename T>
struct compute_size<std::vector<T>> {
    static unsigned of(const std::vector<T>& v) {
        // static size
        unsigned result = sizeof(std::vector<T>);
        // static and dynamic size of all used elements
        for(auto& i : v)
            result += compute_size<T>::of(i);
        // account for allocated empty space
        result += (v.capacity() - v.size()) * sizeof(T);

        return result;
    }
};

template<>
struct compute_size<int> {
    static unsigned of(const int&) {
        return sizeof(int);
    }
};

template<>
struct compute_size<std::string> {
    static unsigned of(const std::string& s) {
        return sizeof(std::string) + s.capacity() * sizeof(std::string::value_type);
    }
};

Used via a function template:

template<typename T>
unsigned get_size(const T& val) {
    return compute_size<T>::of(val);
}

Leading to something like:

std::vector<std::string> qqq;
qqq.push_back("asdfasdf");
qqq.push_back("qwer");
std::cout << get_size(qqq) << std::endl;

With some possible optimisations like:

// for PODs we don't really have to iterate
template<>
struct compute_size<std::vector<int>> {
    static unsigned of(const std::vector<int>& v) {
        return sizeof(v) + v.capacity() * sizeof(int);
    }
};    

And, possibly, generalizing this to whole groups of types using std::enable_if.

2
  • I am getting following error while compiling above code,getmem.cpp:4:22: error: expected initializer before ‘<’ token
    – Deepak
    Nov 26, 2014 at 15:11
  • I would probably need to know what is on line 4 of your code :) However, you are right, that code doesn't compile - partial specializations of functions are still not allowed. I'll fix the answer. Nov 26, 2014 at 16:36
0

Static size : sizeof(vector<S2>)

Dynamic size : myVec.capacity() * sizeof(S2)

5
  • 3
    Nope. Try using your code when S2 is a std::string, for instance. Hint: the result is the number of elements * the sizeof a ptr to a string. I.e a 1 element vector that contains a string object that holds "string index 0" produces an output of 4 (dynamic size).
    – enhzflep
    Nov 25, 2014 at 7:04
  • @enhzflep Do you mean it doesn't include the dynamic size of std::string? Nov 25, 2014 at 7:12
  • 1
    Yup, exactly. Try this: typedef vector<string> vecStr; vecStr myVec; myVec.push_back("This is element 0"); cout << "memory consumed: " << myVec.capacity() * sizeof(string) << " bytes." << endl; The result (in a 32 bit prog on an x64 win7 system) is 4.
    – enhzflep
    Nov 25, 2014 at 7:17
  • @enhzflep I agree. But without a convention, I think it's impossible to calculate the all dynamic size for any type. Nov 25, 2014 at 7:19
  • 1
    :) I'd not be surprised if that was the case.
    – enhzflep
    Nov 25, 2014 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.