Warm tip: This article is reproduced from stackoverflow.com, please click
c tcl

How does `Tcl_Free()` differ from `free()`?

发布于 2020-05-08 01:10:06

The descriptions seem virtually identical. Are there any nuances between the two that should be noted? Why would someone use one over the other? This question may be imposed as well for Tcl_Alloc() and malloc().

Questioner
hkj447
Viewed
32
Donal Fellows 2020-02-21 20:45

They're used because Tcl supports being built on Windows with one tool chain, and loading a DLL built with a different toolchain. A key feature of that scenario is that it is fairly common for different toolchains to have their own implementations of the C library, and that means different implementations of malloc(). You must match malloc() and free() to the same library or you get some truly weird failures (crashes, memory leaks, etc.) By providing Tcl_Alloc and Tcl_Free (which are usually very thin wrappers) it makes it possible for user code to match up the allocations and releases correctly.