-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed as not planned
Labels
A-allocatorsArea: Custom and system allocatorsArea: Custom and system allocatorsA-runtimeArea: std's runtime and "pre-main" init for handling backtraces, unwinds, stack overflowsArea: std's runtime and "pre-main" init for handling backtraces, unwinds, stack overflowsC-discussionCategory: Discussion or questions that doesn't represent real issues.Category: Discussion or questions that doesn't represent real issues.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
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.
Metadata
Metadata
Assignees
Labels
A-allocatorsArea: Custom and system allocatorsArea: Custom and system allocatorsA-runtimeArea: std's runtime and "pre-main" init for handling backtraces, unwinds, stack overflowsArea: std's runtime and "pre-main" init for handling backtraces, unwinds, stack overflowsC-discussionCategory: Discussion or questions that doesn't represent real issues.Category: Discussion or questions that doesn't represent real issues.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.