unsafe

What is the fastest way to convert a float[] to a byte[]?

限于喜欢 提交于 2019-11-26 20:31:27
I would like to get a byte[] from a float[] as quickly as possible, without looping through the whole array (via a cast, probably). Unsafe code is fine. Thanks! I am looking for a byte array 4 time longer than the float array (the dimension of the byte array will be 4 times that of the float array, since each float is composed of 4 bytes). I'll pass this to a BinaryWriter. EDIT : To those critics screaming "premature optimization": I have benchmarked this using ANTS profiler before I optimized. There was a significant speed increase because the file has a write-through cache and the float

What is an “internal address” in Java?

雨燕双飞 提交于 2019-11-26 20:17:27
In the Javadoc for Object.hashCode() it states As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java™ programming language.) It's a common miconception this has something to do with the memory address but it doesn't as that can change without notice and the hashCode() does not and must not change for an object. @Neet Provided a link to a good answer https:/

C# Unsafe/Fixed Code

五迷三道 提交于 2019-11-26 19:57:52
问题 Can someone give an example of a good time to actually use "unsafe" and "fixed" in C# code? I've played with it before, but never actually found a good use for it. Consider this code... fixed (byte* pSrc = src, pDst = dst) { //Code that copies the bytes in a loop } compared to simply using... Array.Copy(source, target, source.Length); The second is the code found in the .NET Framework, the first a part of the code copied from the Microsoft website, http://msdn.microsoft.com/en-us/library

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

萝らか妹 提交于 2019-11-26 19:41:19
问题 Should you use pointers in your C# code? What are the benefits? Is it recommend by The Man (Microsoft)? 回答1: 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 Performance-critical code The use of unsafe

How can I guarantee that a type that doesn't implement Sync can actually be safely shared between threads?

谁说我不能喝 提交于 2019-11-26 17:25:33
问题 I have code that creates a RefCell and then wants to pass a reference to that RefCell to a single thread: extern crate crossbeam; use std::cell::RefCell; fn main() { let val = RefCell::new(1); crossbeam::scope(|scope| { scope.spawn(|| *val.borrow()); }); } In the complete code, I'm using a type that has a RefCell embedded in it (a typed_arena::Arena). I'm using crossbeam to ensure that the thread does not outlive the reference it takes. This produces the error: error: the trait bound `std:

How can I use unsafe code in VB.Net?

我们两清 提交于 2019-11-26 16:56:33
问题 I would like to know the VB.NET equivalent of the following C# code: unsafe { byte* pStart = (byte*)(void*)writeableBitmap.BackBuffer; int nL = writeableBitmap.BackBufferStride; for (int r = 0; r < 16; r++) { for (int g = 0; g < 16; g++) { for (int b = 0; b < 16; b++) { int nX = (g % 4) * 16 + b; int nY = r*4 + (int)(g/4); *(pStart + nY*nL + nX*3 + 0) = (byte)(b * 17); *(pStart + nY*nL + nX*3 + 1) = (byte)(g * 17); *(pStart + nY*nL + nX*3 + 2) = (byte)(r * 17); } } } } 回答1: Looks like it's

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

北城余情 提交于 2019-11-26 16:16:34
This question already has an answer here: How can I get the memory location of a object in java? 4 answers In Java, is there a way to get reference address, say String s = "hello" can I get the address of s itself , also, can I get the address of the object which reference refers to? 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

Why does sun.misc.Unsafe exist, and how can it be used in the real world? [closed]

本小妞迷上赌 提交于 2019-11-26 12:34:10
I came across the sun.misc.Unsafe package the other day and was amazed at what it could do. Of course, the class is undocumented, but I was wondering if there was ever a good reason to use it. What scenarios might arise where you would need to use it? How might it be used in a real-world scenario? Furthermore, if you do need it, does that not indicate that something is probably wrong with your design? Why does Java even include this class? examples VM "intrinsification." ie CAS (Compare-And-Swap) used in Lock-Free Hash Tables eg:sun.misc.Unsafe.compareAndSwapInt it can make real JNI calls into

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

会有一股神秘感。 提交于 2019-11-26 11:22:00
问题 What is the implementation for this function: fn unbox<T>(value: Box<T>) -> T { // ??? } The only function in the documentation that looks like what I want is Box::into_raw . The following will type check: fn unbox<T>(value: Box<T>) -> T { *value.into_raw() } This gives the error error[E0133]: dereference of raw pointer requires unsafe function or block . Wrapping it in an unsafe { ... } block fixes it. fn unbox<T>(value: Box<T>) -> T { unsafe { *value.into_raw() } } Is this the correct

C# unsafe value type array to byte array conversions

假装没事ソ 提交于 2019-11-26 11:16:32
问题 I use an extension method to convert float arrays into byte arrays: public static unsafe byte[] ToByteArray(this float[] floatArray, int count) { int arrayLength = floatArray.Length > count ? count : floatArray.Length; byte[] byteArray = new byte[4 * arrayLength]; fixed (float* floatPointer = floatArray) { fixed (byte* bytePointer = byteArray) { float* read = floatPointer; float* write = (float*)bytePointer; for (int i = 0; i < arrayLength; i++) { *write++ = *read++; } } } return byteArray; }