Taking this signature for a method of the GlobalAllocator:
unsafe fn alloc(&self, layout: Layout) -> *mut u8
and this sentence from the method's documentation:
- The allocated block of memory may or may not be initialized.
Suppose that we are going to allocate some chunk of memory for an [i32, 10]. Assuming the size of i32 it's 4 bytes, our example array would need 40 bytes for the requested storage.
Now, the allocator found a memory spot that fits our requirements. Some 40 bytes of a memory region... but... what's there? I always read the term garbage data, and assume that it's just old data already stored there by another process, program... etc.
- What's unitialized memory? Just data that is not initialized with zeros of with some default value for the type that we want to store there?
- Why not always memory it's initialized before returning the pointer? It's too costly? But the memory must be initialized in order to use it properly and not cause
UB. Why then doesn't comes already initialized? - When some resource it's deallocated, things musn't be pointing to that freed memory. That's that place got zeroed? What really happens when you
deallocatesome piece of memory?