473,320 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Finding out the total memory used for allocating an object

sfs
Hi All,

Let's say I want to find out the total memory consumed by the following
class A. Please note that I don't want the size of the class.

Class A
{
private:
B* mwm1;
C* mem2;
}

Now class B and class C are classes which may contain some more
pointers to objects. For example,

class B
{

private:
int *k;
float *l;
D* m; //D is a class
}

class C
{

private:
int *k;
float *l;
E *g; //E is a class.
}

Regards.
Subhransu

Oct 9 '06 #1
3 2147
sfs wrote:
Let's say I want to find out the total memory consumed by the following
class A.
There is no portable way to get this information. Your code is expected to
know its own sizes and such - that's a feature of C++.

Non-portably, you could use a function with a name like heapwalk(). Examine
your heap before and after new-ing the object and all its dependents.

Now why do you need to know this information? A memory profiler, such as
Valgrind, can tell you about all your memory blocks...

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Oct 9 '06 #2
How about this way:

class Know_your_size
{
public:
virtual int get_heap_size() const=0;
virtual ~Know_your_size() {}
};

class A: public Know_your_size
{
private:
B* mwm1; //both B and C too inherit from Know_your_size
C* mem2;
public:
int get_heap_size() const
{
return mwm1->get_heap_size() + mem2->get_heap_size();
}
};

And so on.

Oct 9 '06 #3
Of course, a std::string doesn't inherit from Know_your_size.

So a final implementation of get_heap_size() could look like this:

class F:public Know_your_size
{
private:
std::string str;
int i;
public:
int get_heap_size() const
{
return sizeof(int) + sizeof(std::string) +
str.size()*sizeof(char);
}
};

Of course, this will only provide you a vague amount of the used
memory. There is most likely much more memory allocated dynamically by
the string class which can't be accessed "legally".

Oct 9 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: soni29 | last post by:
hi, i'm reading a c++ book and noticed that the author seems to allocate memory differently when using classes, he writes: (assuming a class called CBox exists, with member function size()): //...
5
by: Jason Callas | last post by:
I was doing starting some experimenting with the GC and ran into the following odd result. I created an object and my used memory went up by almost 11k but when I cleared it and forced a collection...
1
by: JHenstay | last post by:
I have some questions regarding memory and C++ Objects. For these questions, I'll use the following sample for explanation sake: ------------------------------------ class CBase { public:...
3
by: music4 | last post by:
Greeting, My platform is Solaris 2.8. My question is that if there is a simple way to put a class object in share memory, so that multiple process can use this object. Thanks in advance! Evan
11
by: aaaaa | last post by:
Hi all, Does anybody know if STLPort or SGI STL standard allocators do memory pooling for the list, map and set? Also I have had a look at the BOOST pool_alloc (to be used as a pooling...
8
by: Berk Birand | last post by:
Hi all, I have to use C-style structures for an assignement. I cannot have any methods or constructors for it. What has surprised me is that in my code, I have to allocate memory for an array of...
4
by: Tony Johansson | last post by:
Hello Experts!! I have an easy question when you allocate memory by using new you allocate memory dynamically but what is it called when you allocate memory without using new. Here is an...
12
by: ira2402 | last post by:
Hi All, We are developing sw for a small embedded OS and we have limited memory. We are looking for algorithms, links, and articles about this. The goal is efficient utilization of small amount...
7
by: boss_bhat | last post by:
Hi all , I am beginner to C programming. I have a defined astructure like the following, and i am using aliases for the different data types in the structure, typedef struct _NAME_INFO {...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.