How to get address of struct (mut problems)


Previously was in function:

 let lpPaintStruct: LPPAINTSTRUCT =
         libc::malloc(mem::size_of::<PAINTSTRUCT>() as libc::size_t) as *mut PAINTSTRUCT;

this was disadvantages:

  • need malloc whereas it can be on stack
  • need free otherwise memory leak
  • need unsafe

I try:
let mut PaintStruct: PAINTSTRUCT;
and let mut lpPaintStruct: LPPAINTSTRUCT = &PaintStruct;
or let mut lpPaintStruct: *mut PAINTSTRUCT = &PaintStruct;

I have error:
mismatched types [E0308]:
expected type *mut winapi::PAINTSTRUCT
found type &winapi::PAINTSTRUCT
types differ in mutability


To get a local on the stack, you can do:

let paint_struct: PAINTSTRUCT = unsafe { mem::uninitialized() };

And then get its address for passing to an FFI function:

let paint_struct_ptr = &mut paint_struct as *mut _ as *mut PAINTSTRUCT;


Is uninitialized, and how init this with zeros?