Tcl_Alloc(3)
π Tcl_Alloc β the Tcl Library Procedures glow-up π
okay bestie, sit down, hydrate, because we're talking about memory and honestly? it's giving essential. this one lives in the Tcl Library Procedures section of the manual, page Tcl_Alloc(3), and it's the whole girlboss squad of allocation routines: Tcl_Alloc, Tcl_Free, Tcl_Realloc, Tcl_AttemptAlloc, Tcl_AttemptRealloc, and Tcl_GetMemoryInfo. no cap, they're all here.
β¨ SYNOPSIS β¨
like, here's the whole cast list, exactly as written:
#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're trying to allocate. yes queen, bytes.void *ptr(in) β pointer to the memory block you wanna free or realloc. the ex, basically.Tcl_DString *dsPtr(in) β an initializedDStringpointer. she must be initialized, okay? don't come to the function with an uninitialized DString, that's not the vibe.
π DESCRIPTION π
so these procedures give you a platform and compiler independent interface for memory allocation. that's the whole point, and it's iconic. if your program needs to transfer ownership of memory blocks between Tcl and other modules, you use these routines rather than the native malloc() and free() routines from the C run-time library. periodt.
Tcl_Alloc returns a pointer to a block of at least size bytes, suitably aligned for any use. at least! she might give you more. generous.
Tcl_Free makes the space referred to by ptr available for further allocation. bye babe, your space is up for grabs now.
Tcl_Realloc changes the size of the block pointed to by ptr to size bytes and returns a pointer to the new block. the contents will be unchanged up to the lesser of the new and old sizes β so if you shrink, you lose the tail, if you grow, the new bits are uninitialized vibes. the returned location may be different from ptr, so do NOT go assuming it's the same address, that's how you get a use-after-free situation and it's not cute. if ptr is NULL, this is equivalent to calling Tcl_Alloc with just the size argument. glow-up behavior.
Tcl_AttemptAlloc and Tcl_AttemptRealloc are identical in function to Tcl_Alloc and Tcl_Realloc, except β and this is the important part, bestie β Tcl_AttemptAlloc and Tcl_AttemptRealloc will not cause the Tcl interpreter to panic if the memory allocation fails. if the allocation fails, these functions will return NULL. so you gotta check. no cap. also 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, platform dependent, check your return values fr fr.
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. so the name you call is not always the function you get, iykyk. π
Tcl_GetMemoryInfo appends a list-of-lists of memory stats to the provided DString. 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. so many prerequisites, she's high maintenance but worth it.
β οΈ reminder bestie:Tcl_AttemptAllocandTcl_AttemptReallocreturnNULLon failure instead of panicking, so the checking is on you. andTcl_GetMemoryInfoisn't available in stub-enabled extensions at all, and needs the threaded memory allocator. don't be shocked, it's in the docs. π«Ά
π KEYWORDS π
alloc, allocation, free, malloc, memory, realloc, TCL_MEM_DEBUG
<div align="center">
okay that's the whole page, you ate it, you understood it, main character energy achieved β¨
this was Tcl 9.0, page Tcl_Alloc(3), and yes she's serving the full manual-procedures fantasy πΈ
π¦ go free some memory responsibly, love u π
</div>
π¦ where this page comes from
- alpine 3.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- nixos 26.05: tcl 9.0.3
- nixpkgs unstable: tcl 9.1.0
- 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