[英]Returning a pointer to a structure from function
I use some library which declares some opaque types. 我使用了一些声明了一些不透明类型的库。 I wrapped this type into my own structure to abstract over this library opaque type and got some problem.
我将这种类型包装到我自己的结构中,以抽象出这个库的opaque类型,并遇到了一些问题。 Here is code:
这是代码:
typedef struct my_struct my_struct;
struct my_struct{
opaque_lib_t *const opaque_lib_ptr; //opaque_lib_t is the opaque type
//came from the library
};
my_struct* initialize(){
opaque_lib_t *opaque_lib_ptr;
init(&opaque_lib_ptr); //library function call
return opaque_lib_ptr;
}
void use_and_release(my_struct *my_struct_ptr){
//use and release my_struct
}
my_struct *my_struct_ptr = initialize();
use_and_release(my_struct_ptr); //crashes
In such an implementation the call to use_and_release
crashes. 在这样的实现中,对
use_and_release
的调用崩溃了。 So I tried to replace my_struct* initialize
with the following implementation 所以我尝试用以下实现替换
my_struct* initialize
my_struct* initialize(){
opaque_lib_t *opaque_lib_ptr;
init(&opaque_lib_ptr);
my_struct *my_struct_ptr = malloc(sizeof(*my_struct_ptr));
my_struct tmp = {.opaque_lib_ptr = opaque_lib_ptr};
memcpy(my_struct_ptr, &tmp, sizeof(tmp));
return my_struct_ptr;
}
With such an implementation it works fine. 有了这样的实现,它工作正常。 But I don't understand why the first did not work.
但我不明白为什么第一个没有用。 I thought that the pointer to a structure and the pointer to its first element has the same value.
我认为指向结构的指针和指向其第一个元素的指针具有相同的值。 So in this case it should be fine to just return
opaque_lib_t*
and cast it to my_struct*
since my_struct*
contains only one element. 所以在这种情况下,只返回
opaque_lib_t*
并将其opaque_lib_t*
为my_struct*
因为my_struct*
只包含一个元素。
Why you try to do with your first example is kind of attempting to emulating inheritance from object-oriented languages. 为什么尝试使用第一个示例尝试模拟面向对象语言的继承。 It seems like you want
my_struct
to have an "is a" relationship with opaque_lib_t
. 您似乎希望
my_struct
与opaque_lib_t
具有“is a”关系。
However that won't work because then you need the first member of my_struct
to be an actual instance of the opaque_lib_t
structure, ie it should be 但是这不会起作用,因为那时你需要
my_struct
的第一个成员成为opaque_lib_t
结构的实际实例 ,即它应该是
struct my_struct{
opaque_lib_t opaque_lib_instance; //opaque_lib_t is the opaque type
//came from the library
};
If opaque_lib_t
really is an anonymous and opaque structure similar to FILE
then that's not possible. 如果
opaque_lib_t
确实是一个类似于FILE
的匿名和不透明结构,那么这是不可能的。
Another way of looking at is like this: 另一种看待方式是这样的:
In memory my_struct
will look something like 在内存中
my_struct
看起来像
my_struct opaque_lib_t +----------------+ +-------------------+ | opaque_lib_ptr | -> | Unknown data | +----------------+ | More unknown data | | ... | +-------------------+
You simply can't overlay opaque_lib_t
on top of my_struct
. 您根本无法在
my_struct
上覆盖opaque_lib_t
。
And when you do return opaque_lib_ptr;
当你确实
return opaque_lib_ptr;
you effectively are saying that "this pointer is pointing to a my_struct
whose first member is a pointer to opaque_lib_t
". 你实际上是在说“这个指针指向一个
my_struct
它的第一个成员是指向opaque_lib_t
的指针”。 And that is simply wrong because those are two very different structure. 这是完全错误的,因为这是两个非常不同的结构。
Continuing with the first piece of code, if you try to use my_struct_ptr->opaque_lib_ptr
then the memory you access is the initial data of the opaque_lib_t
structure (whose pointer you returned). 继续第一段代码,如果您尝试使用
my_struct_ptr->opaque_lib_ptr
那么您访问的内存是opaque_lib_t
结构的初始数据(您返回的指针)。
Lastly about what you say 关于你说的话
In my code I want to abstract over the specific library structures
在我的代码中,我想抽象出特定的库结构
I can understand that, but that's already the purpose of opaque_lib_t
. 我能理解,但这已经是
opaque_lib_t
的目的了。 You add an (unnecessary) abstraction on top of an abstraction. 您在抽象之上添加(不必要的)抽象。
I could understand your structure if it collected multiple related data, but not only the opaque_lib_t
pointer. 如果它收集了多个相关数据,我可以理解你的结构,但不仅仅是
opaque_lib_t
指针。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.