`` I am not an expert on compilers and I might be completely stupid sharing an approach for memory safety. I primarily code in c/c++. From what I understand is both language and memory allocator should work together for memory safety. My approach is that pointers store a handle(number) instead of address. Alloc returns this handle. Memory allocators store this handle - (free bool flag, actual address). Compiler can generate instructions to call a allocator function or check a table to validate whether this handle is valid one or not and use the address. For every memory access in heap we have to check if its valid. Not sure if its terribly slow or not. Because handles are 32 bit numbers, and they wrap over, even if an address is reused, that handle will have context of whether its freed or not. (handle 32 bit - (free bool flag(1bit), address(32/64bit)). We could optimize with bits and instructions to do this check or some cpu tricks. int *ptr = malloc(32); // ptr contains a handle ex: 3 *ptr // compiler generates to code to call a function which is defined in allocator library which checks this handle and return the address if valid else raise panic. free(ptr); //allocator just updates the handle free bool flag as true. ptr = malloc(32) // ptr now contains a new handle ex: 4. same address can be returned but a different handle. So any code which still had the previous ptr will panic. Run
Probably this could be used for existing c/c++/nim codebases. My concern is mainly on the performance. Will this work ? ``