简体   繁体   English

在C中动态加载结构的大小

[英]Dynamically loading the size of a structure in C

I have a file named Point.c which has a structure defined named as: 我有一个名为Point.c的文件,该文件的结构定义为:

Point

Also, it has functions in it named point_str, point_deserialize. 此外,它还具有名为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. 现在,我使用dlopen在另一个文件中动态调用这些函数。

So, if I find that the type is Point, I will create a string: 因此,如果发现类型为Point,我将创建一个字符串:

point_str

and call the function by giving the path to the .so file using dlopen. 并通过使用dlopen提供.so文件的路径来调用该函数。

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. 就像dlopen一样,但是要知道结构的大小。

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. 因此,从命令行开始反序列化Point对象。 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 因此,使用该字符串,我想知道位于Point.c中并具有libpoint.so的Point对象的实际大小。

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). .so不会知道结构的大小,除非您使用.so可用的函数对其进行编码(如Jonathan在评论中所述)。 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. 在编译期间会计算所有相关的指针/内存偏移,并且会丢弃用于计算这些偏移的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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