From help-octave-request at bevo dot che dot wisc dot edu Thu Feb 1 18:37:47 2001 Subject: Dynamically linked C/C++ functions and file descriptor From: Joshua Rigler To: Help-Octave Date: Thu, 01 Feb 2001 17:37:45 -0700 If some of my terminology and jargon is not quite correct, please forgive me, and if you're feeling really generous, enlighten me... 1) Suppose I create a dynamically linked C++ function using mkoctfile. This function dynamically allocates some memory, and sets a static pointer for this memory location (using calloc). The function exits, and I'm returned to Octave. I call that function again. Can I assume that the static pointer is still valid and points to the proper place in memory, as it would if I was simply calling the function from another C++ function? 2) Maybe related...suppose I have a C++ function that generates a void pointer file descriptor (i.e. typedef void *CDFid). If I return that file descriptor to the parent Octave session, then use it in subsequent calls to other dynamically linked functions that use it, is that pointer still valid? I realize that if I were actually making a system call, that the pointer would not be valid from one call to another (since the system's memory manager [de]allocates the stack and heap each time the external function is called), but I'm assuming that with dynamically linked functions, Octave and the functions share the same memory space until Octave is terminated. 3) Finally, how the heck can I return a pointer to Octave? It doesn't seem to like anything but double floats and strings (and vectors, arrays, and complex versions of such). Could I perhaps pass back a string of the hexidecimal format of the pointer, and then pass this between function calls, which would convert the string to the real pointer/address...is this even possible? This seems like a tricky problem, but somebody must have done something similar when dealing with file I/O. Any help here would be appreciated. -EJR P.S. Here is my C++ routine. Note the external C libraries. Everything works if I don't try to assign the value in "id" to "retval". As it is here, it doesn't even compile (I'm using RH Linux 7.0 and gcc/g++). // cdf_open -- function to open a CDF file and return the file descriptor // (this is a dynamically linked function for Octave, written in C++) // // Usage: id = CDF_OPEN('filename.cdf'); #include extern "C" { #include #include } DEFUN_DLD (cdf_open, args, nargout, "id = CDF_OPEN('filename.cdf');\n\ \n\ Open a file.;\n\ ... other CDF_ commands ...") { // The list of values to return. See the declaration in oct-obj.h octave_value_list retval; // Simple check to see if we were called correctly int nargin = args.length (); if (nargin == 0 || nargout > 1) { print_usage("cdf_open"); return retval; } // The values to be returned from the CDFlib routines CDFid id; // CDF identifier CDFstatus status; // Status code char *CDFname; // pointer for Variable length CDF filename // Copy string from args into a charactar array that can be // read by CDFopen int flength = args(0).string_value().length(); CDFname = (char *)(calloc(flength,sizeof(char))); strcpy(CDFname,args(0).string_value().c_str()); // Make call(s) to CDFlib status = CDFopen (CDFname, &id); if (status != CDF_OK) printf ("\nError, status: %d\n",status); // **FAILS** assign id to return value retval (0) = id; // free the memory for CDFname free(CDFname); return retval; } ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------