C# Unsafe/Fixed Code

It’s useful for interop with unmanaged code. Any pointers passed to unmanaged functions need to be fixed (aka. pinned) to prevent the garbage collector from relocating the underlying memory. If you are using P/Invoke, then the default marshaller will pin objects for you. Sometimes it’s necessary to perform custom marshalling, and sometimes it’s necessary to … Read more

Is there a way to get a reference address? [duplicate]

You can get the object index with Unsafe. Depending on how the JVM is using the memory (32-bit addresses, 32-bit index, 32-bit index with offset, 64-bit address) can affect how useful the object index is. Here is a program which assumes you have 32-bit index in a 64-bit JVM. import sun.misc.Unsafe; import java.lang.reflect.Field; import java.util.Arrays; … Read more

How do I get an owned value out of a `Box`?

Dereference the value: fn unbox<T>(value: Box<T>) -> T { *value } There’s a nightly associated function into_inner you can use as well: #![feature(box_into_inner)] fn unbox<T>(value: Box<T>) -> T { Box::into_inner(value) } Way back in pre-1.0 Rust, heap-allocated values were very special types, and they used the sigil ~ (as in ~T). Along the road to … Read more

Should you use pointers (unsafe code) in C#?

From “The Man” himself: The use of pointers is rarely required in C#, but there are some situations that require them. As examples, using an unsafe context to allow pointers is warranted by the following cases: Dealing with existing structures on disk Advanced COM or Platform Invoke scenarios that involve structures with pointers in them … Read more

Java 8 Unsafe: xxxFence() instructions

Summary CPU cores have special memory ordering buffers to assist them with out-of-order execution. These can be (and typically are) separate for loading and storing: LOBs for load-order buffers and SOBs for store-order buffers. The fencing operations chosen for the Unsafe API are were selected based on the following assumption: underlying processors will have separate … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)