cisco / ChezScheme

Chez Scheme
Apache License 2.0
6.91k stars 982 forks source link

malloc/foreign-free and foreign-alloc/free #718

Closed bjornkihlberg closed 10 months ago

bjornkihlberg commented 10 months ago

Can foreign-free in Chez Scheme free memory allocated in C++ with malloc and can free in C++ free memory allocated with foreign-alloc in Chez Scheme or are the different systems maintaining their own list of allocated memory such that responsibility of the memory can't be transferred between them?

// Regards Björn Kihlberg

jltaylor-us commented 10 months ago

foreign-alloc and foreign-free call foreign procedures s_malloc and s_free, which in turn call malloc and free from the C library. Whether the C++ functions std::malloc and std::free use the same C library functions is a different question, but I assume all major implementations do even if it's not technically required by the C++ standard.

Of course, this is not a documented behavior of Chez Scheme, so theoretically it could change in the future... but I would find that very surprising.

bjornkihlberg commented 10 months ago

Thank you for the answer! This helps me understand what is going on under the hood.