简体   繁体   中英

Dynamically loading the size of a structure in C

I have a file named Point.c which has a structure defined named as:

Point

Also, it has functions in it named point_str, point_deserialize.

Now, there are similar files like these like

LinkedList.c, Node.c etc with similar definitions.

Now, I am calling these functions dynamically in another file by using dlopen.

So, if I find that the type is Point, I will create a string:

point_str

and call the function by giving the path to the .so file using dlopen.

But, I am not able to know the size of the structure dynamically. Is there a way to do it? I want to do something like

int size = givemesize("Point", "path_to_so");

Which is just like dlopen but to know the size of a structure.

Edit: why I want to know the size?

With that size I am deserializing anything from string to its actual type. That's why I need the size. So, from command line it comes to deserialize a Point object. So, with that string I want to know the actual size of the Point object which resides in Point.c and has a libpoint.so

No, you cannot.

The .so doesn't know the size of the structure unless you encode it in a function available from the .so (like Jonathan said in the comments). All that information is discarded at compile time. Any relevant pointer/memory offsets are computed during compilation and the information used to compute those offsets is thrown away.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM