[Solved]Best way to initialize c void pointer?

Now, I use Box::into_raw(Box::new(0)) as *mut , is any more simple way? or Is rust has the biggest pointer ?

I'm not really sure what you are after but if you want a null pointer you can use null_mut in std::ptr - Rust

1 Like

void pointer is not null pointer;
I think rust always need void pointer to initialize opaque structs pointer,Isn't it?

After search and read some FFI code, I think std::mem::uninitialized() is what I was looking for.

If you are doing C FFI, any void* you see should be accompanied with some information about its true size and/or type. Concrete examples:

  • void zero_memory(void *s, size_t len); would require the void pointer to point to an arbitrary type which size is at least len bytes. In this case void * is no different from char *. Depending on the exact function, it may be possible to put any type there.
  • void *new_opaque_struct(void); would return some opaque struct (likely allocated somehow) which type is an implementation detail. (It is a bad style because there is a way to give a type a name and not a body, but there exists such a code anyway.) In this case you are not expected nor allowed to know its contents; just call an appropriate free_opaque_struct later, preferably at Drop implementations.
  • void get_some_opt(enum opt_type type, void *target); would set target different types and values according to type. This does not map well to Rust; you should have independent functions per each possible type, and fix the type for target.

thanks yous reply, my question may not clear, rust void pointer seems to be some confusion. look below code, my question just how to initialize ptr , The key is we need an opaque_struct pointer , but we couldn't construct an opaque_struct instance, so i think Rust need a general pointer just like c void pointer. but now i know could use std::mem::uninitialized bypasses Rust's normal memory-initialization checks, so there are no simply way to initialize general pointer are acceptable.
(Just think, if has a void-ptr() return an general pointer which could safe transform for any type pointer )

enum opaque_struct {};
let ptr:*mut opaque_struct = ....


This would mean that you need an inside information to create it yourself, and even in C you cannot make one without that information. If you have a source code for the target library, you can try to emulate that (probably there should also be the true struct definition there, so you can copy it). Otherwise, ugh, good luck---you will need some serious reverse engineering.

Not you have misconstrued me ,I don't need opaque_struct, just opaque_struct pointer, in c just simple declare struct opaque_struct *ptr but in rust if no std::mem::uninitialized() initialize this opaque_struct pointer are ugly and not guaranteed( rust must initialize , c don't need).

You can't have an unnamed enum, but you almost got it -- try: enum opaque_struct {}
This is exactly what bindgen creates for opaque structs too. For the pointer, just use std::ptr::null_mut() as suggested already, or get a non-null pointer from whatever C API does know about this type.

1 Like

yes, is enum opaque_struct {}std::ptr::null_mut<T>() could be an right and simple candidate,i had not noticed null_mut<T> are generics type before.

thanks all, I make this question become complicated.