Number of bytes in an object (C++)

Started by
9 comments, last by GameDev.net 17 years, 10 months ago
I'm trying to figure out how I can check the amount of bytes in an object. Since the object is of a templated class, I cannot just use the sizeof operator on the class. Here is the function where I am trying to do this:

template <class Classy> void ExampleClass::ExampleFunc(Classy object, unsigned char byteVariable)
{
	TemplatedClass <Classy> data(byteVariable, object); //Makes an object containing byteVariable and object
	//////must find how many bytes in data right here
}




Since byteVariable is one byte, could I just use " sizeof(Classy) + 1 "? Or can you not use sizeof on types you are using through a template? Or does this have a different problem? I'd like to know before I try it because I wouldn't be able to test if it worked 'till I add a bunch of other stuff. Thanks. Edit: I know sizeof won't necessarily return the amount of bytes (according to the C++ standard) but in most cases it should, so good enough. Edit2: Not trying to sound mean or anything, but please do not try to answer without reading the source code (including the comment) or knowing what a template is. Seriously, there have been too any replies that say "if you can see what variables are in the class just add up in your head how many bytes each type of varible in the class takes." However funny this is, it really is just a waste of space. [Edited by - PunaProgrammer chris on June 17, 2006 5:20:05 AM]
Advertisement
C++ template worked like C macro.

When compiler reach a templated call(that is use templated class), it replace templated class's text into a certain type then compile it.

So, I think you can use sizeof without worry.
Do you have access to the TemplatedClass and the Classy classes? If so you could just look in them, find out how big they are in bytes (by calculating the size of their respective member variables).

If this is an exercise, and that's not a possibility then I'm not sure I have a good answer for you.

I would hope that sizeof() is written to work with templated classes, meaning it's smart enough to first find the size of the template parameter, then to find the size of the class when using that specific template type. If that's the case then go ahead and use it (maybe someone can verify or negate my hope?).

Good luck
It should be fairly obvious why you can't take the size of a tempated class. The paramters to the template could be of any size, and it is not permitted by the standard.

Although you cannot use the sizeof operator on the class type, you can create an object instance and take its size. That will include the actual size of the object given that particular template variables, along with any padding added by the system for alignment and other reasons.

The actual size is going to vary not just on the type, but also by compiler and even by compiler options once you factor in other differences like RTTI implementations, different packing and alignment requirements, optimizations, and other factors.
To the best of my knowledge (I can't quote chapter and verse, but I do know how these things work in some detail, and can't think of any reason why it wouldn't work), sizeof(TemplatedClass<Classy>) will work just fine.

But note that sizeof always works with *static* types; it's a compile-time operator. And anyway, needing sizeof() is usually a sign that you're doing something yourself that you shouldn't have to be doing :/ (I can only think of one legitimate use offhand, which is for wrapping the iostream binary .read()/.write() interface to read primitives rather than just char[] buffers - naturally, you need to worry about endianness and struct padding when you do that).
Quote:Original post by Zahlman
I can only think of one legitimate use offhand, which is for wrapping the iostream binary .read()/.write() interface to read primitives rather than just char[] buffers - naturally, you need to worry about endianness and struct padding when you do that).

Other legitimate uses:

  • Computing the size of arrays when (for any of several reasons) you cannot use one of the collection classes
  • Any serialization, not just through iostream
  • Custom allocators and memory pools
  • Porting and communicating between machines with different type sizes
  • Taking advantage of implementation-defined behavior for low-level (assembly) optimizations, used in conjunction with offsetof macro
  • Working with derived classes (doesn't really apply in this case, but it is a legitimate use.)
  • Template metaprogramming. The Boost MPL libraries use sizeof for several things
  • Interfacing with legacy systems

I'm sure there are more. The sizeof operator is still needed in C++, and isn't just a holdover from C.
... well yes, I spoke too broadly. But beginners shouldn't be concerned with *most* (if any; serialization seems most likely and is related to my example, but you can do text serialization too) of those things particularly often :)
Okay, I will try the sizeof(TemplatedClass<Classy>) method. The actual thing I need it for is network coding (the send function of RakNet needs the amount of bytes you are sending). Thanks for the help.

you could make the class calculate it's own size after it's created.basicly if this class
isn't going to be reused by any other programmers. you should have a pretty good idea
about the contents of the class (assuming that nothing is going to be dynamic) . you can
then calcualte it manualy and just store in the app.
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction."
Quote:Original post by PunaProgrammer chris
Okay, I will try the sizeof(TemplatedClass<Classy>) method. The actual thing I need it for is network coding (the send function of RakNet needs the amount of bytes you are sending). Thanks for the help.


You should never send classes directly across a network for a bunch of reasons. An incopmlete list of reason includes the possibility of erroniously sending a derived class, sending non-transferrable RTTI and sending vtables. Also, you should never directly transfer anything with either virtual entries or containing pointers. Both will result in errors on the other side since pointers are not transferrable.

Fill out a packed, plain old data structure (PODS) with the data you need. Copy the data from the class to the data structure. Compress and transfer the data structure as you see fit. Then decompress and re-create the object on the other side.

This topic is closed to new replies.

Advertisement