Thread overview | |||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
August 11, 2013 Get class size of object | ||||
---|---|---|---|---|
| ||||
Given an object, is there a built in way to get the size of the class the object represents? The object may be instantiated with a derived instance of the object type, so using classInstanceSize doesn't work on the type of the object. I can obviously go through the trouble of adding a class member and override it in all the derived classes. I saw tsize in object but 2.063 says it doesn't exist. |
August 11, 2013 Re: Get class size of object | ||||
---|---|---|---|---|
| ||||
Posted in reply to JS | On 11.08.2013 06:25, JS wrote:
> Given an object, is there a built in way to get the size of the class
> the object represents? The object may be instantiated with a derived
> instance of the object type, so using classInstanceSize doesn't work on
> the type of the object.
>
> I can obviously go through the trouble of adding a class member and
> override it in all the derived classes. I saw tsize in object but 2.063
> says it doesn't exist.
>
There is no general way to do this at compile time in any language.
|
August 11, 2013 Re: Get class size of object | ||||
---|---|---|---|---|
| ||||
Posted in reply to JS | On 08/11/2013 06:25 AM, JS wrote:
> Given an object, is there a built in way to get the size of the class
> the object represents?
Yes.
|
August 11, 2013 Re: Get class size of object | ||||
---|---|---|---|---|
| ||||
Posted in reply to JS | On 8/11/13, JS <js.mdnq@gmail.com> wrote:
> Given an object, is there a built in way to get the size of the class the object represents?
Try:
import core.memory;
auto size = GC.sizeOf(object);
|
August 11, 2013 Re: Get class size of object | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | On Sunday, 11 August 2013 at 13:40:41 UTC, Timon Gehr wrote:
> On 08/11/2013 06:25 AM, JS wrote:
>> Given an object, is there a built in way to get the size of the class
>> the object represents?
>
> Yes.
Troll.
|
August 11, 2013 Re: Get class size of object | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tobias Pankrath | On Sunday, 11 August 2013 at 09:30:32 UTC, Tobias Pankrath wrote:
> On 11.08.2013 06:25, JS wrote:
>> Given an object, is there a built in way to get the size of the class
>> the object represents? The object may be instantiated with a derived
>> instance of the object type, so using classInstanceSize doesn't work on
>> the type of the object.
>>
>> I can obviously go through the trouble of adding a class member and
>> override it in all the derived classes. I saw tsize in object but 2.063
>> says it doesn't exist.
>>
>
> There is no general way to do this at compile time in any language.
Is this a mathematical fact that you have proven for all languages? I'd like to see your proof if it's not bigger than what will fit in a post.
|
August 11, 2013 Re: Get class size of object | ||||
---|---|---|---|---|
| ||||
Posted in reply to JS | > Is this a mathematical fact that you have proven for all languages? I'd like to see your proof if it's not bigger than what will fit in a post.
That's so obvious, that you should be able to come up with one yourself. Maybe you never experienced a situation where it was clear, but that doesn't mean I need to come up with a proof, just because you are to lazy to do it yourself.
|
August 11, 2013 Re: Get class size of object | ||||
---|---|---|---|---|
| ||||
Posted in reply to JS | On Sunday, 11 August 2013 at 15:28:44 UTC, JS wrote:
> On Sunday, 11 August 2013 at 13:40:41 UTC, Timon Gehr wrote:
>> On 08/11/2013 06:25 AM, JS wrote:
>>> Given an object, is there a built in way to get the size of the class
>>> the object represents?
>>
>> Yes.
>
> Troll.
I guess he does not answer to the question as well as others do (including me) because of your misbehavior in D newsgroups. Solution to your question is obvious (built-in feature without any import). By the way, GC.sizeOf seems to return size of allocated page which is bigger than needed (for ex. it returns 16 for a new int which is 4).
|
August 11, 2013 Re: Get class size of object | ||||
---|---|---|---|---|
| ||||
Posted in reply to Maxim Fomin | Maxim Fomin:
> GC.sizeOf seems to return size of allocated page which is bigger than needed (for ex. it returns 16 for a new int which is 4).
The object instance contains a pointer to the virtual table, so there's a way to know what object it is. With that runtime information it should be possible to know the static size of the instance.
Bye,
bearophile
|
August 11, 2013 Re: Get class size of object | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tobias Pankrath | On Sunday, 11 August 2013 at 15:38:04 UTC, Tobias Pankrath wrote:
>> Is this a mathematical fact that you have proven for all languages? I'd like to see your proof if it's not bigger than what will fit in a post.
>
> That's so obvious, that you should be able to come up with one yourself. Maybe you never experienced a situation where it was clear, but that doesn't mean I need to come up with a proof, just because you are to lazy to do it yourself.
No, but your simple wrong. Either you have no clue what you are talking about, don't understand the issue, or are intentionally stating falsehoods.
It's very easy to manually implement a way to get the class size by just using a virtual function that returns the class size. But do you think I should have to waste my time adding the same function to every class to make it work? Of course you do.
|
Copyright © 1999-2021 by the D Language Foundation