Monday, May 24, 2010

C++ How do i find memory used by an object?

I have a class


class A


{


public:


list%26lt; int %26gt; myList;


}





sizeof( A) does not change even when i add items into myList. Is there another way to get the memory used by an object?

C++ How do i find memory used by an object?
sizeof() is a compile-time keyword that is not going to have any effect at runtime. It is replaced by the size of the class when the program is being compiled, so it is never going to change. You need to do something like this.


...


{


A my_obj;


int total_size = sizeof(my_obj) + sizeof(int)*my_obj.myList.size();


}
Reply:It does answer your question, you just don't realize it. There isn't a way to use sizeof() or any other function. If you have variables like containers or dynamic memory, you have to manually count that memory. Nothing will give you the size of the object and all of its child objects. Report It



No comments:

Post a Comment