Sizeof(<class>)?

Hey,

my last question for today :smiley: is, how can i properly get the total byte-amount of a class-type/instance?

Is it even possible?

Should i then write an own implementation of it?

Hope you guys can help me out :slight_smile:

–Shpend

what’s your end goal? what do you want to do with that number?

I actually Need this, to get the exact object-sizet of a byte-stream back :slight_smile:

And for this, it would be cool to have a sizeof(class or interface);

Interfaces don’t have a size. They’re views on a class. You can’t write a class to a stream since it contains pointers.

But what’s with this:

Couldnt I just go ahead and interate over the propertys of a class and get the sizeof each valuetype within? (ofc when it contains just value types)
And give the ultimate “sum” back to the caller?

sure. But that doesn’t account for alignment and what would you do with say strings (Which aren’t value types), what would you even do with the resulting number as it seems rather useless?

I could even get the char out of every string and save this to a variable, like:

var amount  := 0;
for c in text do
begin
  inc(amount, sizeof(c));
end;

The Thing is, what i am more concerned about this, do I have to account the “Memory-Layout” of the internal representation of the class? Or the padding, packing?

It all depends on what you’re doing. You shouldn’t deal with the size or padding of a class in the first place imo. Instead you should loop the properties and/or fields of the class, write them out in sequence, only dealing with types you actually support, based on the type info gotten from the RTTI apis.

Ok, but as i said in the beginning, i want to be able to get, for instance, a “MyCar = class” out of a Byte-stream and for that i Need the size of it.

Thats the only reason, because when the Client/user wants to write a myCar(class) in the stream, which has a natural object-state, example, Color, size, Price, Name etc… his good right is, to serialize this in a binary Format, because not every user-defined-object is a record, sometimes its also needed to save a classinstance to a stream not just records, IMO

that’s not going to work. classes are complicated things that contain pointers and other stuff. you can’t just write a class to a stream, and later read it back — most of its members (any that aren’t simple types), and all of its internal metadata will be wring, because the pointers don’t point to the right things, as you only saved and reloaded the address, but not the things they point to.

you need to serialize this in a well defined format you define yourself. by serializing all the class’s members, down to simple types. for example, say your Car class has three fields number of wheels, color and license plate. the number and color might be an int and an enum, you can just ave those. but the license plate will be a string. you will need to serialize the string itself, burt the class’s memory only contains the pointer to the string object, e.g.:

Wheels = 4
Color = Red
License Plate = 0x43fbeefcace

when you save this and reload it, 0x43fbeefcace will point to garbage memory.

does that make sense?

Ok, I got it.

I will try to serialize it in a well defined Format :slight_smile:
Thanks!