Size of an object in memory at runtime

The C++ sizeof operator can give you the object size at compile time: int size = sizeof(MyClass) For most classes this shouldn't differ from the run time size unless you are doing some weird stuff with memory. Only gotcha is that sizeof needs to have the correct type so if you do sizeof on the base class but at runtime what you have is a derived class with additional data then it won't be accurate. You could work around this by adding a virtual size method to your class hierarchy where each derived class returns the result of the sizeof operator on itself. Also if your class has other objects that it allocates dynamically and points to then those sizes are not taken into account. You would have to write code to traverse all of them and sum up the sizes. If you share more details about what you are trying to accomplish I'm happy to offer other ideas. Josh On 28/11/11 11:12 AM, skunkworks-request@lists.my.co.ke wrote:
Message: 1 Date: Mon, 28 Nov 2011 10:56:23 +0300 From: James Nzomo<kazikubwa@gmail.com> To: Skunkworks Mailing List<skunkworks@lists.my.co.ke> Subject: [Skunkworks] Size of an object in memory at runtime Message-ID: <CAChuKueA0LoOZKGhyKmQUrd6OrOGUgJTKoTrRMiOQUOvfHpz6A@mail.gmail.com> Content-Type: text/plain; charset="iso-8859-1"
Has anyone here successfully (and accurately) determined the size of a C++ class instance (Object) in memory during run time?
participants (1)
-
Josh Handley