MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Mathlink: How do I pass arbitrary data from Mathematica to C?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg86901] Re: Mathlink: How do I pass arbitrary data from Mathematica to C?
  • From: Szabolcs Horvát <szhorvat at gmail.com>
  • Date: Wed, 26 Mar 2008 04:47:38 -0500 (EST)
  • Organization: University of Bergen
  • References: <fs4rmb$p3s$1@smc.vnet.net>

On Mar 23, 7:00 am, Karen Bindash <KarenBind... at googlemail.com> wrote:
 >
 > int g(const void *data, long nbytes);

...

 > I don't want Mathematica to try to interpret the data in any way -
 > just to pass an address of where the data is in memory, and also the
 > number of bytes of data. Than the library function g will return an
 > integer, which I want to pass back to Mathematica.

I think that there is a misunderstanding here.  Everyone has assumed in 
their replies that you would like to transmit the data that const void 
*data is pointing to between the C program and Mathematica.  However, it 
seems to me that the data is actually handled by the C program, and 
*never* by Mathematica.  So Mathematica should only know about very 
simple objects that somehow *refer to* this "special" object that is 
stored in memory by the C program (and not Mathematica).

Please clarify this important point.

If this is the case, then one could use a Mathematica object 
someObject[ptr, len], where ptr and len are integers, and encode the 
pointer to the data and the length of the data.  Only this someObject[] 
object would be passed to the C program (easily achievable with .tm 
templates), which in turn would decode the two values (ptr and len) to 
obtain a pointer and a length.  This would work for as long as all the 
someObject[] objects are constructed by the C program.  But if the user 
examines the structure of someObject[] and tries to construct 
someObjects with arbitrary ptr and len values, then the C program will 
get some invalid pointers, and will most likely crash.

So I would suggest maintaining a table on the C side, which translates 
some identifier (e.g. a single integer) to a (ptr, len) pair.  The 
identifier would be used on the Mathematica side, and the C program 
could check for validity of identifiers before translating them to 
pointers, to avoid invalid pointers and crashes.


  • Prev by Date: Re: Basic plotting of an evaluated function
  • Next by Date: Re: Tagged list processing
  • Previous by thread: Re: Mathlink: How do I pass arbitrary data from Mathematica to C?
  • Next by thread: Re: Mathlink: How do I pass arbitrary data from Mathematica to C?