Size of an object in memory at runtime

I see. You are right that sizeof won't work in this case since you need to get the size of the instance itself plus all the dynamically allocated objects (strings and pointers). Sizeof can give the size of an object but of the object and all the objects it points too. There is no simple way to do that in C++. You would have to write code to take the sizeof the object itself and then add all the sizes of the objects that it points and the sizes of the objects that they point too etc... But even if you could do that, it still wouldn't help with the problem you have. The other objects that your object points too (strings, char pointers...) will not be in the same contiguous memory block as the object itself. So even if you copy your object onto the clipboard and allocate enough space for the other objects, the other objects won't get copied onto the clipboard. Here is an example of what I mean: Say you have your drag and drop object DDObj that has a pointer to a Child object: class Child { int i; } class DDObj { Child* c; } When you create the objects, each one gets created with a call to new: DDObj dd = new DDObj(); dd.c = new Child(); // this might actually happen inside DDObj constructor So instance dd might be at memory location 100 but since dd.c was created with a different call to new it could be at totally different memory location - 200 or 1000 or 10000 - anywhere. So even though you know that the size of DDObj plus the size of c is say 16 bytes they aren't 16 bytes next to each other. You probably have 8 bytes in one place and the other 8 bytes in another. So if you try to copy 16 bytes onto the clipboard starting at the location of dd you will end picking some other values for dd.c. Even if you did get lucky and the objects were lined up in memory when the other application at the other end of the clipboard got your object the pointer to dd.c would probably no longer be valid since the object would likely have been remapped to a different virtual address space. To do this using the in memory representation of your objects you would have to either restrict the objects on the clipboard to simple value types and fixed size arrays (no std::string, no char*) or you would have to manage the memory yourself using some kind of variant of a mempool. If I were you I would look for a library that you can use to marshal/serialize your objects. Not sure if there is anything like for use with wxWidgets. Using a JSON or XML representation of the objects is one way to serialize them although you may be able to find something slightly more efficient for an in memory representation. In any case, it isn't something you want to code yourself as remapping the pointers is fairly tricky. Josh On 28/11/11 1:19 PM, skunkworks-request@lists.my.co.ke wrote:
Message: 3 Date: Mon, 28 Nov 2011 13:01:49 +0300 From: James Nzomo<kazikubwa@gmail.com> To: Skunkworks Mailing List<skunkworks@lists.my.co.ke> Subject: Re: [Skunkworks] Size of an object in memory at runtime Message-ID: <CAChuKucuy6DM5ofjp-uoOdjG-=AQjZkFwnSNZE3sohOrUrHpkA@mail.gmail.com> Content-Type: text/plain; charset="iso-8859-1"
I'm using wxWidgets. It's clipboard mechanism extends the underlying platform's clipboard mechanism.
The object is of a custom class. This class contains various datatypes ranging from ints,doubles,std::strings,char pointers to custom typedefs, structs and other classes.
The objects i want to D&D are of different types. I will have a clipboard data object for each type as opposed to having an abstract implementation which is more complex.

Actually you're right. Non contiguous storage is a big barrier to the "ellegant" shortcut I so wished for. A somewhat hidden disadvantage for unmanaged versus managed languages. Anyway I have decided to go with the JSON serialization route (since I was going to employ JSON for IPC anyway). Back to the UML drawing board :-(. I think I'll have both *void serialize(std::string&)* and *bool unserialize()* member functions for every D&D-able class, with the nested (un)serialization calls for their class type members. Serialize on begin drag and unserialize it a new object on drop to recreate its state. One clear & consoling advantage of this is that you can drag an item onto text editor and the json string will be pasted there. Advantegeous for debugging & customer support tools :-). _______________________________________________ *Good judgement comes from Experience.* *Most of that comes from Bad Judgement.* _______________________________________________ * * 2011/11/28 Josh Handley <josh@bridgeinternationalacademies.com>
I see. You are right that sizeof won't work in this case since you need to get the size of the instance itself plus all the dynamically allocated objects (strings and pointers). Sizeof can give the size of an object but of the object and all the objects it points too. There is no simple way to do that in C++. You would have to write code to take the sizeof the object itself and then add all the sizes of the objects that it points and the sizes of the objects that they point too etc...
But even if you could do that, it still wouldn't help with the problem you have. The other objects that your object points too (strings, char pointers...) will not be in the same contiguous memory block as the object itself. So even if you copy your object onto the clipboard and allocate enough space for the other objects, the other objects won't get copied onto the clipboard. Here is an example of what I mean:
Say you have your drag and drop object DDObj that has a pointer to a Child object:
class Child { int i; }
class DDObj { Child* c; }
When you create the objects, each one gets created with a call to new:
DDObj dd = new DDObj(); dd.c = new Child(); // this might actually happen inside DDObj constructor
So instance dd might be at memory location 100 but since dd.c was created with a different call to new it could be at totally different memory location - 200 or 1000 or 10000 - anywhere. So even though you know that the size of DDObj plus the size of c is say 16 bytes they aren't 16 bytes next to each other. You probably have 8 bytes in one place and the other 8 bytes in another. So if you try to copy 16 bytes onto the clipboard starting at the location of dd you will end picking some other values for dd.c. Even if you did get lucky and the objects were lined up in memory when the other application at the other end of the clipboard got your object the pointer to dd.c would probably no longer be valid since the object would likely have been remapped to a different virtual address space.
To do this using the in memory representation of your objects you would have to either restrict the objects on the clipboard to simple value types and fixed size arrays (no std::string, no char*) or you would have to manage the memory yourself using some kind of variant of a mempool.
If I were you I would look for a library that you can use to marshal/serialize your objects. Not sure if there is anything like for use with wxWidgets. Using a JSON or XML representation of the objects is one way to serialize them although you may be able to find something slightly more efficient for an in memory representation. In any case, it isn't something you want to code yourself as remapping the pointers is fairly tricky.
Josh
On 28/11/11 1:19 PM, skunkworks-request@lists.my.**co.ke<skunkworks-request@lists.my.co.ke>wrote:
Message: 3 Date: Mon, 28 Nov 2011 13:01:49 +0300
From: James Nzomo<kazikubwa@gmail.com> To: Skunkworks Mailing List<skunkworks@lists.my.co.ke**> Subject: Re: [Skunkworks] Size of an object in memory at runtime Message-ID: <CAChuKucuy6DM5ofjp-uoOdjG-=AQ**jZkFwnSNZE3sohOrUrHpkA@mail.** gmail.com <AQjZkFwnSNZE3sohOrUrHpkA@mail.gmail.com>> Content-Type: text/plain; charset="iso-8859-1"
I'm using wxWidgets. It's clipboard mechanism extends the underlying platform's clipboard mechanism.
The object is of a custom class. This class contains various datatypes ranging from ints,doubles,std::strings,char pointers to custom typedefs, structs and other classes.
The objects i want to D&D are of different types. I will have a clipboard data object for each type as opposed to having an abstract implementation which is more complex.
______________________________**_________________
Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/**mailman/listinfo/skunkworks<http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks> ------------
Skunkworks Rules http://my.co.ke/phpbb/**viewtopic.php?f=24&t=94<http://my.co.ke/phpbb/viewtopic.php?f=24&t=94> ------------ Other services @ http://my.co.ke
participants (2)
-
James Nzomo
-
Josh Handley