Re: [pure-lang-users] C float
Status: Beta
Brought to you by:
agraef
From: Albert G. <Dr....@t-...> - 2008-08-13 19:53:09
|
John Cowan wrote: > Casting away const is always safe: const essentially documents how the callee > uses the argument or its referent. Exactly. The most important thing you have to keep in mind is that if a C routine returns a const pointer, you're not supposed to modify the data in Pure. And if a C routine takes a const pointer as argument, you can pass it any pointer from Pure (malloced memory, string, etc.) and be confident that the C routine won't modify it. Pure also takes care of marshalling Pure strings from/to the system encoding if the corresponding C type in the Pure extern declaration is char*. That usually does the right thing for const char* arguments and return values on the C side. Where things get a bit messy is when you have to pass *non-const* pointers as arguments or get them as results. A non-const pointer _argument_ is most likely used to return results; in that case you'll usually have to malloc some memory to be passed for that parameter, maybe initialize the data as required by the C function and decode the results and free the memory after the function returns. A non-const pointer _return value_ (which isn't also a non-const parameter, such as in strcpy) often indicates data malloced by the C routine which is supposed to be freed by the caller (i.e., the Pure program). Albert -- Dr. Albert Gr"af Dept. of Music-Informatics, University of Mainz, Germany Email: Dr....@t-..., ag...@mu... WWW: http://www.musikinformatik.uni-mainz.de/ag |