Skip to content

do zero-sized allocations have to obey alignment requests? #42794

Closed
@pnkfelix

Description

@pnkfelix

Consider the following code (playpen):

fn main() {
    use std::mem;
    let array_u8 = Box::new([0_u8; 0]);
    let array_u64 = Box::new([0_u64; 0]);
    let addr_u8 = &*array_u8 as *const _ as usize;
    let addr_u64 = &*array_u64 as *const _ as usize;
    
    println!("alignof  [u8; 0] {:?}", mem::align_of::<[u8; 0]>());
    println!("alignof [u64; 0] {:?}", mem::align_of::<[u64; 0]>());
    println!("array_u8 addr: {:?} array_u64 addr: {:?}", addr_u8, addr_u64);
    
    assert!(addr_u8 % mem::align_of::<[u8; 0]>() == 0);
    assert!(addr_u64 % mem::align_of::<[u64; 0]>() == 0);
}

In Rust stable, this assert-failed. In Rust beta and nightly, it passes. (From what I can, this change in behavior is due to #41064 )

So maybe there's no bug at all. But I wanted to at least have a place to track discussion of whether this particular change is something we want to require of all user allocators, rather than forcing it to be intermixed with other discussion on #32838

(Thanks to Ralf Jung for pointing out this question about allocator behavior on IRC.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions