Questions tagged [unsafe]

In C# or Rust, the unsafe keyword marks code able to work directly with memory pointers, bypassing some of the language's safety checks. In Java, `sun.misc.Unsafe` is a special class performing low-level, unsafe operations.

In C# The unsafe keyword denotes an unsafe context, which is required for any operation involving pointers. You can use the unsafe modifier in the declaration of a type or a member. Once so declared, the entire textual extent of the type or member is considered an unsafe context.

For more information, see Unsafe Code and Pointers (C# Programming Guide)

The unsafe Keyword on MSDN
http://msdn.microsoft.com/en-us/library/chfa2zb8(v=vs.100).aspx


In Java, sun.misc.Unsafe is a special class performing low-level, unsafe operations. Though this is a private platform API, this class is used extensively in many projects and libraries including Hadoop, Spark, Guava, Cassandra, etc.


In Rust, the unsafe keyword denotes a function or block that is implemented outside some of the safeguards of the compiler. Thus it is as much an escape hatch as a device to easily document code that might require increased attention. More information can be found in the Rust Book


In Go, package unsafe contains operations that step around the type safety of Go programs. Packages that import unsafe may be non-portable and are not protected by the Go 1 compatibility guidelines.

990 questions
0
votes
1 answer

constant-time set operations

Are there constant-time algorithms for binary set intersection and union? I imagine using bitmaps with pointers to elements in the memory and using OR for union and AND for intersection. Does anyone now of a solution?
Cetin Sert
  • 4,497
  • 5
  • 38
  • 76
0
votes
2 answers

Is there a speed benefit to using unsafe code in .Net to swap objects of complex types?

Here is my current swap code for swapping 2 KeyValuePair objects in an array: KeyValuePair t = a[i]; a[i] = a[j]; a[j] = t; Would there be any speed advantage to using unsafe code and merely swapping the pointers of…
IamIC
  • 17,747
  • 20
  • 91
  • 154
0
votes
1 answer

Fastest way to sort byte array using unsafe code?

According to my knowledge quicksort is one of the fastest sorting algorithms, since thats how the Array.Sort() function is implemented in the framework. Is there a way to speed the sorting of a byte array up, probably using unsafe code and pointers?
yq8
  • 145
  • 1
  • 10
0
votes
1 answer

Java bytecode variable index 0's className is something strange

I use ASM library to generate bytecodes and load them using Unsafe.defineAnonymous as a Class. Both work in most of cases, but after for a short time, it fails. Then I add some debug instructions in the emitted bytecodes to print something, and the…
shijie xu
  • 1,975
  • 21
  • 52
0
votes
1 answer

How unsafe is unsafe code?

I'm reading about unsafe code, primarily for working with Bitmaps. I can't find, however, an explanation of the limits of unsafeness. I understand that using pointers on an array will not be checked so I might try accessing memory outside of its…
ispiro
  • 26,556
  • 38
  • 136
  • 291
0
votes
2 answers

Unsafe State and Safe State

I have learned a little about Unsafe State and Safe State. Safe State is when there is no chance of deadlock occuring, while unsafe state doesn't mean a deadlock has occurred yet, but means that a deadlock could happen. What I'm trying to figure out…
0
votes
1 answer

c# improve binary serialization

I am currently working on a project that generates code based on other code. Essentially using the programming language itself as a DSL. One of the generator targets is a binary DataContract serializer and the generated ToBytes for the following…
Toxantron
  • 2,218
  • 12
  • 23
0
votes
2 answers

Java Unsafe Operations with Combo Boxes

I know this question has been asked before, but I cannot find a solution to the issue of combo boxes. I have the following code: ... JComboBox startingCombo = new JComboBox(); startingCombo.setModel(new DefaultComboBoxModel(new String[] {"USD",…
Jonathon M
  • 71
  • 5
0
votes
1 answer

Bitmap Manipulation - image width is reduced by 1/4

The guy prior to me on my project wrote some code I try to understand. I'm not really good in C# but try to get my hold of it. The following code is intended to apply a threshold to the color values but right now i'm trying to simply copy the image…
AllDayPiano
  • 414
  • 1
  • 4
  • 20
0
votes
1 answer

Performance tips mapping indexers to objects

Given the following code I am able to access the pixels of an image and map them to a Color struct. To my eye though this looks inefficient since every time I access the indexer I am reading from the array four times. It takes about 9 seconds on my…
James South
  • 10,147
  • 4
  • 59
  • 115
0
votes
1 answer

Is this method of copying a string faster than copying each char individually?

Is this method of copying a string faster than copying each char individually? The idea of this code is that it could (but I am not sure if it is so) be faster to copy 8 bytes at once instead of a single byte. It is very un-safe but it seems to work…
user5280366
0
votes
0 answers

Set opacity of particular pixels (and these radius) using Bitmap.LockBits and BitmapData

I'm trying to set opacity of surroundings of particular pixels in bitmap. For now I can set opacity of pixels that meets some conditions. (e.g. pixel on left or rigt or top or down is not 100% white) I'm using Bitmap.LockBits and BitmapData, becouse…
0
votes
1 answer

Java: How to get an object from the HEAP?

This is the scenario: I have instantiated a String in a class, then I have obtained its position in HEAP: public class UnsafeExperiment { static String s = "ciao"; public UnsafeExperiment() throws Exception { Unsafe unsafe =…
Andrea T
  • 3,035
  • 4
  • 23
  • 39
0
votes
2 answers

Clearing a char from memory

While i can easily get a pointer of a char from a string: public static unsafe void Clear(this string s) { fixed (char* charPtr = s) { for (int i = 0; i < s.Length; i++) charPtr[i] = '\0'; } } I…
U. Bulle
  • 1,075
  • 9
  • 20
0
votes
0 answers

Can you consume/reuse unsafe .NET types from other packages/dlls in F#?

I am aware that F# does not currently have an unsafe scope built into the language. If I have an unsafe type (written in C#) and subsequently compiled as a library for reuse in F#, how (conceptually) would I go about using that unsafe type (if at…
whitebox
  • 75
  • 5