Solved

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

Posted on 2015-01-24
4
100 Views
Last Modified: 2015-02-11
How much memory requires class object? How to determine that?
0
Comment
Question by:Nusrat Nuriyev
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
basic hardware to learn oop advanced design patterns 3 110
computer science syllabus 3 100
Installshield for Embarcadero EX 10.1 Berlin 4 72
Gaming Software 1 29
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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

756 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