Tcl_Alloc(3tcl)
π NAME π
Tcl_Alloc, Tcl_Free, Tcl_Realloc, Tcl_AttemptAlloc, Tcl_AttemptRealloc, Tcl_GetMemoryInfo β bestie, these are the girls who allocate and free heap memory π
So this is a page from the Tcl Library Procedures manual, and honestly? It's giving memory-management main character energy. Let's get into it β¨
πΈ SYNOPSIS πΈ
#include <tcl.h>
void *
Tcl_Alloc(size)
Tcl_Free(ptr)
void *
Tcl_Realloc(ptr, size)
void *
Tcl_AttemptAlloc(size)
void *
Tcl_AttemptRealloc(ptr, size)
Tcl_GetMemoryInfo(dsPtr)
π ARGUMENTS π
size_t size(in) β the size in bytes of the memory block you wanna allocate. Like how many bytes, gorl.void *ptr(in) β pointer to the memory block you're freeing or realloc'ing.Tcl_DString *dsPtr(in) β an initializedDStringpointer. Yes it has to be initialized already, we don't do uninitialized in this house π
β¨ DESCRIPTION β¨
Okay so here's the tea β: these procedures give you a platform and compiler independent interface for memory allocation. If your program needs to hand off ownership of memory blocks between Tcl and other modules, you use these routines β not the native malloc() and free() from the C run-time library. No cap.
Tcl_Allocreturns a pointer to a block of at leastsizebytes, suitably aligned for any use. Any use. She's versatile like that πTcl_Freemakes the space referred to byptravailable for further allocation. She's done, she's releasing, she's moving on π¦Tcl_Reallocchanges the size of the block pointed to byptrtosizebytes and returns a pointer to the new block. The contents stay unchanged up to the lesser of the new and old sizes. Heads up tho β the returned location may be different fromptr. IfptrisNULL, this is equivalent to callingTcl_Allocwith just thesizeargument. Glow-up or relocation, either way she ate π₯Tcl_AttemptAllocandTcl_AttemptReallocare identical in function toTcl_AllocandTcl_Realloc, except β and this is the important part β they will not cause the Tcl interpreter to panic if the memory allocation fails. If the allocation fails, these functions returnNULL. Lowkey chill about it, highkey useful π
Note that on some platforms, but not all, attempting to allocate a zero-sized block of memory will also cause these functions to return NULL. So likeβ¦ zero bytes, zero vibes, sometimes zero pointer. Not everywhere tho. iykyk π
When a module or Tcl itself is compiled with TCL_MEM_DEBUG defined, the procedures Tcl_Alloc, Tcl_Free, Tcl_Realloc, Tcl_AttemptAlloc, and Tcl_AttempRealloc are implemented as macros, redefined to be special debugging versions of these procedures. (Yes, that spelling is exactly what the page says. We do not fix the source material, we respect the source material π«Ά)
Tcl_GetMemoryInfo appends a list-of-lists of memory stats to the provided DString. But listen β this function cannot be used in stub-enabled extensions, and it is only available if Tcl is compiled with the threaded memory allocator. When used in stub-enabled embedders, the stubs table must be first initialized using one of Tcl_InitSubsystems, Tcl_SetPanicProc, Tcl_FindExecutable or TclZipfs_AppHook. Read that twice bestie, it's a whole prerequisite situation π
π KEYWORDS π
alloc, allocation, free, malloc, memory, realloc, TCL_MEM_DEBUG
That's the whole page, you're officially fluent in Tcl heap memory π
This was pulled from Tcl 9.0 β so like, that's the version, that's the source, that's the moment. Thanks for reading with me, bestie, mwah π π π¦ β¨
π¦ where this page comes from
- debian trixie: tcl9.0-doc 9.0.1+dfsg-2
- ubuntu questing: tcl9.0-doc 9.0.2+dfsg-1
- ubuntu resolute: tcl9.0-doc 9.0.3+dfsg-1
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 1995-1996 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms