简体   繁体   English

通过mex函数返回C结构

[英]Returning C struct through mex-function

I am trying to return a struct created in mex function to matlab. 我试图将在mex函数中创建的结构返回给matlab。 Now there is a function mxCreateStructArray but I am creating a node-type structure for a tree data structure and thus it is a recursive linked list formed. 现在有一个函数mxCreateStructArray但是我正在为树数据结构创建一个节点类型的结构,因此它是一个递归链表。 Also, the nodes are formed by dynamic memory allocation. 而且,节点是由动态内存分配形成的。 So, it is not possible for me to preallocate the memory. 因此,我无法预分配内存。 I am at a loss as to how I can transport this struct formed to matlab-type struct as output of the function. 我不知道如何将形成的该结构传输为matlab类型的结构作为函数的输出。

If you were working in strictly C, the solution would be to use pointers. 如果您使用严格的C语言,解决方案将是使用指针。 Pointers allow you to tell your program "Hey, I'm going to pass you this memory address, I tell you what this address is, and you do stuff for me without you having to work on it by yourself" . 指针使您可以告诉程序“嘿,我要向您传递该内存地址,我告诉您该地址是什么,您就可以为我做一些事情,而无需您自己进行操作” When pointers are used in this regard, they can be quite powerful, capable of changing the values of constants. 在这方面使用指针时,它们可能非常强大,能够更改常量的值。 However this power is often abused or misused, so many languages put restrictions on the use of pointers, or create their own workarounds to implement their capabilities. 但是,这种功能经常被滥用或滥用,因此许多语言对指针的使用施加了限制,或者创建了自己的变通办法来实现其功能。

MATLAB does have some pointer functionality , however your approach to returning the entire tree is bad practice. MATLAB确实具有一些指针功能 ,但是您返回棵树的方法是不好的做法。 That much information, let alone efficiency of that is dangerous in more ways than one. 这么多的信息,更不用说效率了,它所带来的危险远不止一种。 A function that returns the entire tree of nodes can be useless or even unnecessary information. 返回整个节点树的函数可能是无用的,甚至是不必要的信息。 You should try redesigning your program such that its starting from the head node, recursively going down the tree and returning each node from tail up. 您应该尝试重新设计程序,使其从头节点开始,递归地从树上向下,然后从尾部向上返回每个节点。

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

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