Skip to content

buffer for stdout is heap-allocated #139006

Open
@abgros

Description

@abgros

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

No one assigned

    Labels

    A-allocatorsArea: Custom and system allocatorsA-runtimeArea: std's runtime and "pre-main" init for handling backtraces, unwinds, stack overflowsC-discussionCategory: Discussion or questions that doesn't represent real issues.T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions