Hey,
my last question for today 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
âShpend
Hey,
my last question for today 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
â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
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
Thanks!