The memory pointed to by Ptr a allocated by malloc lives on the heap, as in the C programming language. It is ignored by the garbage collector - you must manually free it yourself using free , and be careful not to use it again after that.
alloca f performs a similar distribution, calls f with a pointer, and frees memory after that. Pointer should not be used after return f .
These routines are not intended to be used in everyday code, but only to interact with other languages ββusing a C-like interface (FFI). You get exactly the same guarantees of memory security. Sentence C - which has virtually no. Thus, it is quite dangerous and should be used with great care.
For comparison, memory marked ForeignPtr is still stored in the heap, but garbage will be collected after there are no more pointers (i.e. Haskell ForeignPtr a ) to memory. Please note that even if garbage collection is used here, such pointers are not risk-free. Indeed, when Haskell no longer has pointers to live memory, the runtime will free it, even if that pointer still lives in a foreign language. The programmer must make sure that this never happens.
chi
source share