Re-allocating properly (program crashes)

    /// Wraps around a pre-existing value
    pub fn wrap<'a, T: Sized>(mut t: T) -> InformationResult<'a, Self> {
        let ptr = (&mut t as *mut T) as *mut u8;
        let layout = Layout::for_value(&t);
        println!("I get this far");
        let ptr_new = unsafe{ std::alloc::realloc(ptr, layout, layout.size()) };
        println!("... But this doesn't print");
        std::alloc::set_alloc_error_hook(|_| {
            println!("No error appears here, even though it crashes");
        Ok(Self {
            ptr: ptr_new,
            len: size,
            cursor: 0,
            read_version: AtomicUsize::new(0),
            write_version: AtomicUsize::new(0),
            corrupt: false,

The program fails at the realloc subroutine. 
error: test failed, to rerun pass '-p hypervec --test primary'

Process finished with exit code -1073740940 (0xC0000374)

The re-alloc function states to use the same layout which was assigned for the pointer. I assume for_layout should do the same?

Is it possible to re-allocate without having to Copy around the bytes of the pointee? I am trying to minimize the number of copies and re-allocations overall

At the risk of repeating myself from Stack Overflow, you can't ever realloc a stack pointer. Attempting to do so may corrupt both your stack and your heap.

Sometimes, yes, but it's not guaranteed. And you can't re-allocate in this case because you never allocated to begin with.

1 Like

I figured it would be better to put on here. Had a long night /nosleep

Does this answer imply that the allocator doesn't have a table for memory locations?

Allocators commonly assume you don't pass random pointers to them.

1 Like

You can only pass pointers to realloc that you previously got from an alloc or realloc call.

The allocator might have some sort of table or linked list of memory allocations internally, but you can't access it, and it isn't going to check it on dealloc/realloc, since that'd both be expensive and unneeded, as it expects all pointers given to be valid.


If you want to put the t on the heap, just put it in a Box. Otherwise just store it directly in the struct.


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.