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
276
votes
16 answers

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

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…
pdeva
  • 43,605
  • 46
  • 133
  • 171
157
votes
7 answers

Why do I get the error "Unsafe code may only appear if compiling with /unsafe"?

Why do I get the following error? Unsafe code may only appear if compiling with /unsafe"? I work in C# and Visual Studio 2008 for programming on Windows CE.
Gold
  • 60,526
  • 100
  • 215
  • 315
147
votes
17 answers

How can I use pointers in Java?

I know Java doesn't have pointers, but I heard that Java programs can be created with pointers and that this can be done by the few who are experts in Java. Is it true?
user213038
89
votes
1 answer

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

What is the implementation for this function: fn unbox(value: Box) -> T { // ??? } The only function in the documentation that looks like what I want is Box::into_raw. The following will type check: fn unbox(value: Box) -> T { …
Calebmer
  • 2,972
  • 6
  • 29
  • 36
79
votes
5 answers

When to use pointers in C#/.NET?

I know C# gives the programmer the ability to access, use pointers in an unsafe context. But When is this needed? At what circumstances, using pointers becomes inevitable? Is it only for performance reasons? Also why does C# expose this…
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
74
votes
2 answers

Where is sun.misc.Unsafe documented?

Does anyone know of any comprehensive documentation for sun.misc.Unsafe? I'm looking for documentation on Unsafe.putOrderedInt(). This was all I was able to find. public native void putOrderedInt(Object o, long offset, int x) …
James Raitsev
  • 92,517
  • 154
  • 335
  • 470
65
votes
2 answers

Does it make any difference to use unsafe inside or outside a loop?

I never needed to use unsafe in the past, but now I need it to work with a pointer manipulating a bitmap. I couldn't find any documentation that indicates otherwise, but I would like to understand better how unsafe works and if it makes any…
Dzyann
  • 5,062
  • 11
  • 63
  • 95
63
votes
7 answers

C# performance - Using unsafe pointers instead of IntPtr and Marshal

Question I'm porting a C application into C#. The C app calls lots of functions from a 3rd-party DLL, so I wrote P/Invoke wrappers for these functions in C#. Some of these C functions allocate data which I have to use in the C# app, so I used…
kol
  • 27,881
  • 12
  • 83
  • 120
54
votes
3 answers

Error lnk2026: module unsafe for safeseh image

I got this error when building a sample visual C++ project. First I downloaded 3 sample projects, all solve the same problem, print out all the prime numbers less than N (you may know these sample projects ?). I built the pure-C project without any…
Hoai Dam
  • 658
  • 2
  • 7
  • 13
51
votes
4 answers

Java 8 Unsafe: xxxFence() instructions

In Java 8 three memory barrier instructions were added to Unsafe class (source): /** * Ensures lack of reordering of loads before the fence * with loads or stores after the fence. */ void loadFence(); /** * Ensures lack of reordering of stores…
Alexey Malev
  • 6,408
  • 4
  • 34
  • 52
48
votes
11 answers

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

Should you use pointers in your C# code? What are the benefits? Is it recommend by The Man (Microsoft)?
Kredns
  • 36,461
  • 52
  • 152
  • 203
46
votes
6 answers

C# 'unsafe' function — *(float*)(&result) vs. (float)(result)

Can anyone explain in a simple way the codes below: public unsafe static float sample(){ int result = 154 + (153 << 8) + (25 << 16) + (64 << 24); return *(float*)(&result); //don't know what for... please explain } Note: the above…
gchimuel
  • 527
  • 1
  • 5
  • 9
40
votes
5 answers

Is there a way to get a reference address?

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?
Adam Lee
  • 24,710
  • 51
  • 156
  • 236
34
votes
7 answers

C# Unsafe/Fixed Code

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…
HB
34
votes
2 answers

How to use unsafe code in safe contex?

I need to use SecureString for a Microsoft's class and i found the following code on the internet: public static class SecureStringExt { public static SecureString ConvertToSecureString(this string password) { if (password == null) …
CodeArtist
  • 5,534
  • 8
  • 40
  • 65
1
2 3
65 66