Boost logo

Boost Users :

Subject: Re: [Boost-users] [Serialization] Serialization of function pointers
From: Zachary Turner (divisortheory_at_[hidden])
Date: 2009-11-12 17:04:21


On Thu, Nov 12, 2009 at 4:55 AM, elizabeta petreska <
elizabeta.petreska_at_[hidden]> wrote:

> Hello everybody
> This is my first post to the list.
> Can I serialize C++ function pointer ? I search around and found that it
> could not be done ( although I am not sure ).
> If it can not be done, whats the explanation about it ?
> Thanks
>
>
If you think about it, I mean it makes perfect sense. Consider a more
common example: Serializing a directed graph. Each node of course has
pointers to the nodes it can reach. When you serialize this graph, do you
actually serialize the *pointer*? Of course not, because every time you run
the program it's at a different memory location. So when serializing data
pointers, you have to resort to different techniques. For example, give
each node a unique id, serialize each node, and then the ids of the nodes
that can be reached. On deserialization, reconstruct the transitions from
the ids.

Ultimately you're going to have to do the same thing here: Serialize enough
information that allows you to determine upon deserialization which function
is actually being referred to. For example, if this is an exported
function, you could serialize the name of the module from which it's
exported as well as the name of the function. Then you could reconstruct
this at runtime by using dlopen/dlsym (linux) or LoadLibrary/GetProcAddress
(windows).

If it is not a function exported from any library, but an arbitrary private
function that may point anywhere in your code base (or for that matter in
the code segment of some other module that also happens to be loaded in your
process), then you need to figure out a way to serialize enough information
that you can reconstruct it at runtime and it's going to be complicated,
although if it were really important I can think of a few areas to begin
looking into.



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net