Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
pnkfelix opened this issue Jun 21, 2017 · 1 comment
Closed

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

pnkfelix opened this issue Jun 21, 2017 · 1 comment

Comments

@pnkfelix
Copy link
Member

pnkfelix commented Jun 21, 2017

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.)

@pnkfelix
Copy link
Member Author

Ugh I need to think more before going through the work of filing tickets. Zero-sized allocations are illegal according to the API of trait Alloc. Thank goodness!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant