Solved

Memory optimization

Posted on 2002-07-18
3
219 Views
Last Modified: 2010-04-01
This may seem like an "obvious" or "beginner" question, but I've had no formal training in C++ programming, and I can't find any good information about this particular topic.

I create two classes, ie:  A Person class and a Car Class.
Each person will own two cars.
The car class will have a very small number of member variables (2 or 3), but a large number of functions (20 or 30).
There will be a large number of Person classes created at one time during the program, so there will be twice as many car classes created (2 for each person).

My question is:  How can I determine the memory footprint (not necessarily exactly, but generally) that is going to be taken up by each car class.  If it is going to be heavy because of all the functions, I want to find a different way of going about the whole process.
0
Comment
Question by:dirtdart
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 7163331
The functions (or better: methods) actuallydon't really contribute to the size, as they are stored only once. The "memory footprint" pretty much is determined by the data (member variables) that are stored. To find out about that, use

size_t sz = sizeof ( <datatype>);

The 'sizeof()' operator returns the size, in bytes, of any datatype or class.
0
 
LVL 5

Author Comment

by:dirtdart
ID: 7163336
Well that was quick and painless!  And believe it or not, exactly what I wanted to hear.  I was afraid that the methods would dramatically increase the size of the class, and in this case a large class taking up a lot of memory would be totally unacceptable.

Thanks
0
 
LVL 5

Author Comment

by:dirtdart
ID: 7163337
Well that was quick and painless!  And believe it or not, exactly what I wanted to hear.  I was afraid that the methods would dramatically increase the size of the class, and in this case a large class taking up a lot of memory would be totally unacceptable.

Thanks
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question