Open
Description
While playing around with global allocators, I noticed that this code fails.
use std::alloc::{GlobalAlloc, Layout};
struct StupidAlloc;
unsafe impl GlobalAlloc for StupidAlloc {
unsafe fn alloc(&self, _layout: Layout) -> *mut u8 {
0 as _
}
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {}
}
#[global_allocator]
static GLOBAL: StupidAlloc = StupidAlloc;
fn main() {
println!("Hello world!");
}
A message is printed: memory allocation of 1024 bytes failed
, and then the program terminates. Removing the line with println!
fixes this, so I assume that it is trying to allocate 1 KiB and then crashing when it can't.
However, there's no need to allocate memory here, because the string to be printed is static. So although this isn't strictly a bug, it certainly might be considered a performance issue in a IO-heavy program.