Solved

22. How much memory requires class object? How to determine that?

Posted on 2015-01-24
4
89 Views
Last Modified: 2015-02-11
How much memory requires class object? How to determine that?
0
Comment
Question by:Nusrat Nuriyev
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 333 total points
ID: 40568249
The net size of a class can be obtained using 'sizeof()', just like in C, e.g.

#include <iostream>

class Vehicle {
public:
  void Drive() {speed = 42;}
protected:
  int speed;
};

int main() {

  std::cout << "Sizeo of Vehicle is " << sizeof(Vehicle) << std::endl;
  return 0;
}

Open in new window


But, there's a caveat: When a class contains pointer members whose values are allocated dynamically, that memory isn't taken into account by 'sizeof()' - you would have to provide a member that does account for that memory and reports it.
The same applies to classes that use lists or other containers.
0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 167 total points
ID: 40568684
sizeof gives you the macroscopic answer. To optimize your performance you may wish to take a finer look at the reasons your sizeof result may be higher than desired. You may wish to look closer at the cache alignment and data holes in the class data members. To do this use the Linux pahole utility:
http://linux.die.net/man/1/pahole

The goal is to rearrange your data members to minimize the cache lines, to try to keep data members from requiring two cache lines, and to reduce the number of holes in your data members (i.e., wasted portions of cache lines).
0
 

Author Comment

by:Nusrat Nuriyev
ID: 40604640
Jkr,
The same applies to classes that use lists or other containers.
Did you mean stl containers , iterators?

But, there's a caveat: When a class contains pointer members whose values are allocated dynamically, that memory isn't taken into account by 'sizeof()' - you would have to provide a member that does account for that memory and reports it.

Likewise, usual pointer? it always give's us 4 bytes, right?

Good answer, phoffric!
But still this does not solve the problem of dynamic member data allocation size affected when determining size?
Is it possible to get this info?
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 333 total points
ID: 40604674
>> Did you mean stl containers , iterators?

For example, since these are the most common ones. Or, let's say: std::string

>> Likewise, usual pointer? it always give's us 4 bytes, right?

Yes, exactly. But when you dynamically allocate memory to it, you would have to 'manually' account for that. On the other hand, if you argue that this memory is not 'within' the object... but that is a bit short handed, since teh consumption still persists.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now