Why should it err?
You did nothing to that dangling pointer that should warrant an error/crash.
Never wrote to it, never freed it twice, never dereferenced it.
fmt.println(ptr) is the same as printf("%p", ptr) in C, it never reads the memory the pointer points to, so it’s safe even with nil/NULL.
im new to low level programming so i might be missing something, but isnt printing freed address of the pointer technically a usage of the pointer. I freed the pointer i shouldnt be able to access it anymore it should throw unexpected behaviour or use after free or something.
Does seem strange to still contain the value after freeing. If there’s concern about accidentally using pointer after free, could set it to nil after free as a general habit.
a := new(string)
free(a)
a = nil
fmt.println(a)
shouldnt compiler do it on its own, why am i getting a memory address if im freeing the allocated memory space
Memory allocation and deallocation inside program is just a convention, if memory is not returned to the operating system. Since you haven’t done anything else after freeing memory, your data is still there, but allocator thinks that that memory is free, so if you’ll allocate once more, allocator could provide you the memory you’ve just freed and only after using such memory you could get an error.
Compiler isn’t checking this after you because it’s quite expensive (it needs to verify that every memory access is valid), so by default it trusts you. In fact an error in your code is called “use after free” and compiler has instrumentation to catch such errors, it’s called Address sanitizer. You could compile program with it and you should get an error. But be aware that even address sanitizer has some limitations, so you should be cautious what allocator do you use with it.
No. As far as I know, this is where you got it wrong. Usage of a pointer is when you dereference it. And even that isn’t usually caught by compilers, but rather at runtime.
Edit: Just noticed that you were talking about a runtime error so my second point in this post is not important ![]()
No. Calling free(a) will never change the value of a. I also don’t know of other languages that do that. Calling free only affects the memory that a points to.
To clarify a bit more, I updated the code a bit.
main :: proc() {
a := new(string)
a^ = "hello"
fmt.println(a)
fmt.println(a^)
free(a)
fmt.println(a)
fmt.println(a^)
}
Based on my assumptions, only the last line of this should be able to cause a runtime error. In my quick test on a Windows machine, it doesn’t. It just prints “hello” which is expected because the memory hasn’t been overridden with anything else. When I use odin run . -sanitize:address, that last print still doesn’t crash, but at least just prints random bytes. I don’t know if odin has some debug allocator or build mode that could be used to make this mistake more obvious.
Variable a: ^string here is just a memory address, i.e. uintptr, integer, index of a byte in memory of your computer. So compiler doesn’t really care if it’s freed, it’s still just a number that can be printed
du vonc ases tik jan
