Sign in to follow this  

Memory consumption of a class with vectors.

This topic is 4377 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am using C++. I have some questions relating to the following example classes.
class CApple
{
public:
CApple();//constructor
~CApple();//destructor
void Update();

private:
string m_sName;
int m_nNoWorms;
};

class CItem
{
public:

private:
vector <CApple> m_vctApples;//lists
vector <COrange> m_vctOranges;
vector <CPear> m_vctPears;
};


1)Does it affect performance if a class has a constructor/destructor that has no code in it? 2)Class CItem holds a list of apples, etc. To determine how much memory class CItem takes, does class CItem memory=CApple memory*numberInList + COrange memory*numberInList + CPear memory*numberInList ? 3)If class CItem has empty lists, what is the memory of CItem? Is it CItem memory=CApple memory*1 + COrange memory*1 + CPear memory*1 ?

Share this post


Link to post
Share on other sites
1) No. In release build, the empty function is removed.

2) The memory usage will be in the order of what you describe, but usually a bit more because it would be too slow go grow the vector for every insert. Typically this means that it could be up to double that.

3) I don't think STL containers specify the clas overhead, only the per-item overhead, which is zero for a vector. It doesn't matter anyway, that's always when it's at it's smallest, so who cares.

Basically, don't worry about the memory usage of C++ containers, or how well the compiler makes things fast. Just concentrate on using the most appropriate container to get the job done. Let the compiler writers do the low-level optimisation, you just do the high-level optimisation.

Share this post


Link to post
Share on other sites
Quote:
Original post by utilae
Hi, I am using C++. I have some questions relating to the following example classes.

*** Source Snippet Removed ***
1)Does it affect performance if a class has a constructor/destructor that has no code in it?


Smart compilers should be able to do optimize this away.

Quote:

2)Class CItem holds a list of apples, etc. To determine how much memory class CItem takes, does class
CItem memory=CApple memory*numberInList + COrange memory*numberInList + CPear memory*numberInList ?


No, it will usually be more than that.
1) Padding, there might be some padding which the compiler might be using.
2) vector might be doing some bookkeeping which you are not aware of.
3) Other bookkeeping by the Compiler. For instance if your class was a derived class, there might be a pointer to a virtual function pointer.

Quote:

3)If class CItem has empty lists, what is the memory of CItem? Is it
CItem memory=CApple memory*1 + COrange memory*1 + CPear memory*1 ?


Refer answer to Question 2).

Share this post


Link to post
Share on other sites

template <typename T>
int totalAllocation(const std::vector<T>& v) {
return sizeof(std::vector<T>) + sizeof(T) * v.capacity();
}

int CItem::totalAllocation() {
return totalAllocation(m_vctApples) +
totalAllocation(m_vctOranges) +
totalAllocation(m_vctPears);
}
// by the way, please reconsider the variable names. The whole point
// of using library containers is to *avoid* thinking about which container
// you have.


Edit: Oops, forgot to add in the allocation for the Apple::name, er, CApple::m_sName. Anyway, string provides a function similar to .capacity() I believe, and you can apply the technique recursively as required.

[Edited by - Zahlman on December 19, 2005 5:43:09 PM]

Share this post


Link to post
Share on other sites
It's kind of hard to tell from a fictional example, but in this case I would be far more worried about the overhead of a dynamic string in CApple.
Quote:
Original post by utilae
1)Does it affect performance if a class has a constructor/destructor that has no code in it?
Probably not, it depends how POD types are detected and handled, however in this case it's not really empty since it's implicitly invoking the string's constructor.

Share this post


Link to post
Share on other sites

This topic is 4377 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this