help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Passing pointers from mex->octave->mex


From: Arnaud BARRE
Subject: Re: Passing pointers from mex->octave->mex
Date: Thu, 13 Oct 2011 17:20:13 +0200

On Thu, Oct 13, 2011 at 2:58 PM, Michael Goffioul
<address@hidden> wrote:
> On Thu, Oct 13, 2011 at 1:39 PM, José Vallet <address@hidden> wrote:
>> Hello.
>>
>> I have a C library with which I can create and manipulate some structures.
>> What I would like to do (from octave):
>>
>> 1 - Call the C function that creates the structure, and somehow store a
>> pointer to the structure for later use.
>> 2 - Do some other things
>> 3 - Call another C function from the library that will manipulate the
>> created structure. Therefore I need to pass the pointer to the structure
>>
>> How to make octave to store a pointer to a structure created in a mex file
>> and how to pass that pointer to another mex file?
>
> I had to solve a similar problem and simply stored pointers as uint32
> or uint64 in octave (depending on the architecture). Then the C code
> does conversion to/from actual pointers. Not very elegant, but it works.
>
> There's some discussion about having a proper pointer type in octave,
> but that won't help in real MEX files; this would only be available in
> OCT files.
>
> Michael.
> _______________________________________________
> Help-octave mailing list
> address@hidden
> https://mailman.cae.wisc.edu/listinfo/help-octave
>

You can also check the utility "Mex Object Handle" realized by Tim
Bailey (http://www-personal.acfr.usyd.edu.au/tbailey/software/index.html).
It is in C++, but the main idea is here and the main part of the code
can be translated in C. Moreover, this utility is for Matlab but works
well under Octave (At least with the tests I realized).

You can also use the type double to manage your C pointer instead of
the types uint32 and uint64. It has the benefit to be independant of
the CPU architecture.

The main problem in this solution (as well as the method proposed by
Michael), is the memory management. Octave cannot now the memory used
by your C pointer. So it is important to clear it when it is finished
by invoking another MEX function (which will use the C "free"
function). If you can use C++ code, the solution of Tim Bailey
proposes a garbage collector invoked when you use the command "clear
all". However, this solution doesn't resolve the problem of the memory
management if too many C++ objects exists at the same time in the
workspace (e.g. have a batch which construct a C++ object at each
iteration. Assigning the same variable in the loop won't free the
memory used by the C++ object as Octave doesn't know it). A MEX
function used to clear the memory of the C/C++ pointers is still
necessary.

Arnaud


reply via email to

[Prev in Thread] Current Thread [Next in Thread]