Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::{
- alloc::{alloc as std_alloc, dealloc as std_dealloc, Layout},
- ffi::CStr,
- mem,
- os::raw::c_char,
- };
- unsafe fn as_str<'a>(it: *mut [c_char]) -> &'a str {
- CStr::from_ptr(it as *const _).to_str().unwrap()
- }
- unsafe fn alloc<T>() -> *mut T {
- std_alloc(Layout::new::<T>()) as *mut T
- }
- unsafe fn dealloc<T>(it: *mut T) {
- std_dealloc(it as *mut _, Layout::new::<T>());
- }
- unsafe fn use_it(it: *mut [c_char; 10]) {
- // use it
- println!("{}", as_str(it));
- println!("{:?}", *it);
- // we're done with it, so de-allocate it
- dealloc(it);
- }
- fn main() {unsafe{
- // allocate it
- let it: *mut [c_char; 10] = alloc();
- // initialize it
- *it = *mem::transmute::<_, &_>(b"P455w04d\0\0");
- // use it
- use_it(it);
- // we're done with it, so de-allocate it
- dealloc(it);
- }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement