除C、C++外,支持手动内存分配的编程语言有哪些?
Great question! If you're used to the fine-grained memory control C offers—manual allocation/deallocation, direct memory address access, etc.—there are several languages besides C++ that let you get your hands dirty at that low level. Here are the most notable ones:
Rust
Rust is famous for its ownership system that prevents memory safety issues by default, but it still gives you full manual control when you need it. Usingunsafeblocks, you can work with raw pointers (*const T/*mut T), directly allocate/deallocate memory via thestd::allocmodule, and read/write to specific memory addresses with functions likestd::ptr::readandstd::ptr::write. You can even manually drop heap-allocated values withstd::mem::dropif you want precise control over when memory is freed.D
Designed as a modern alternative to C++, D natively supports manual memory management just like C. You can usemalloc/free(or D's ownnew/deletewhich work similarly), manipulate pointers freely, and disable the optional garbage collector entirely for scenarios where you need absolute control over memory. It also plays nicely with C code, making it easy to reuse existing low-level libraries.Nim
Nim is a compiled language that balances readable syntax with powerful low-level capabilities. It supports manual memory allocation viaalloc/deallocfunctions, raw pointer operations, and lets you toggle its garbage collector on or off. You can directly access specific memory addresses, perform pointer arithmetic, and manage memory lifecycle exactly as you would in C—all while enjoying Nim's cleaner syntax compared to traditional low-level languages.Assembly
This is the ultimate in memory control. Assembly languages (x86, ARM, etc.) let you directly manipulate registers and memory addresses with no abstraction. You're responsible for every byte of memory allocation, deallocation, and access—you can read/write to any memory location the hardware allows. It's extremely low-level and requires deep hardware knowledge, but there's no limit to how strictly you can control memory here.Swift
While Swift primarily uses automatic reference counting (ARC) for memory management, it includes anUnsafeAPI that unlocks low-level memory control. You can createUnsafeMutableRawPointerinstances to allocate memory manually withallocate(byteCount:alignment:), deallocate it withdeallocate(), and directly read/write values to specific memory addresses. This is often used for interacting with C libraries or performance-critical code.Go
Go has a garbage collector, but itsunsafepackage lets you bypass it for low-level operations. You can convert between Go types and raw pointers withunsafe.Pointer, perform pointer arithmetic, and access specific memory addresses. You can also use functions from thesyscallpackage to interact with the OS's memory allocation routines if you need even more control.
It's worth noting that most of these languages offer safe, high-level abstractions by default, but they provide escape hatches (like unsafe blocks) for when you need the kind of memory control C is known for.
内容的提问来源于stack exchange,提问作者AGawish

