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

unreported exception when using Unsafe

I have wrote a simple code to use Unsafe.prefetchRead on an array and used this test code as a template. import sun.misc.Unsafe; import java.lang.reflect.*; public class Arr { static int [] a = new int[3]; static Unsafe unsafe; static int…
mahmood
  • 23,197
  • 49
  • 147
  • 242
0
votes
0 answers

Strange behaviour of sun.misc.Unsafe.put...() on Solaris-sparcv9

I am experiencing JVM crash for following simple code.. import sun.misc.Unsafe; public class TestProgram { static Unsafe unsafe = Util.getUnsafe(); public static void main(String args[]){ long iBlockSize = 3; long…
pradipmw
  • 191
  • 2
  • 11
0
votes
3 answers

What does the symbol * after a type in C#?

I'm new in C# and what I learned at school it's really poor. Last week I came to the same problem, I didn't know what the ? means after a name type in C#. I could find the answer, then again what does "??" means in a expression, I'm really…
Miguel Lattuada
  • 5,327
  • 4
  • 31
  • 44
0
votes
3 answers

Unsafe Error in C#

Hi i try to use some code in my project , a error rise to syntax and i dont know what is it . error come from the tag started with "unsafe" . What is unsafe and Where and Why Should Use It ? tnx friends... the code is here : public Bitmap…
user3290286
  • 687
  • 1
  • 7
  • 8
0
votes
1 answer

Using UnSafe code to make a pointer move to the next element for a custom class

Is it possible to do something like the following in C#? unsafe string GetName() { Foo[] foo = new Foo[2]; // Create an array of Foo and add two Foo elements foo[0] = new Foo { Name = "Bob" }; foo[1] = new Foo { Name = "Jane" }; Foo…
user1515024
  • 181
  • 9
0
votes
1 answer

Singleton To Ensure Password Security

I am making a little project that requires me to access one of my email accounts frequently to send emails. With that being said, I obviously know the log-in information ahead of time, and in order to protect the password from being created into a…
David Venegoni
  • 508
  • 3
  • 13
0
votes
1 answer

Browser Custom Protocol unsafe

Using other helpful answers on the StackExchange community I have a custom URI protocol registered on a client's machine. It points to a simple AppleScript that we can then change as needed. In testing on our Mac here, it works without issue with a…
Tyler Forsythe
  • 1,471
  • 17
  • 22
0
votes
2 answers

unsafe struct Inaccessible error

Structure Definition [StructLayout(LayoutKind.Sequential, Pack = Compile.PackSize)] unsafe struct DB_PREPLIST { public TxnUnion txn; public fixed byte gid[DbConst.DB_XIDDATASIZE]; } Inaccessible error Question When I try to change…
Pankaj
  • 9,749
  • 32
  • 139
  • 283
0
votes
1 answer

Jagged array pinning in c#

I have kind of an issue. I am trying to pin a jagged array (which i am using due to the sheer size of the data i am handling): public void ExampleCode(double[][] variables) { int nbObservants = variables.Length; var allHandles =…
0
votes
2 answers

Direct memory access to underlying field data

I'm looking for a way to avoid FieldInfo.Get/SetValue overhead, and access memory directly for a few select, known ahead of time, primitive types. (Most specifically, I'm looking to avoid any memory allocations in our custom serializer) Basically,…
tbone
  • 166
  • 1
  • 10
0
votes
2 answers

What would happen in an thread unsafe .NET queue object?

I have a .NET queue object. The producer thread do the Enqueue operation, the data enqueued in the queue is a byte[] array, while the other consumer thread do the Dequeue operation on the same queue object. I use locks to handle concurrency. My code…
Sunf71
  • 93
  • 2
  • 8
0
votes
2 answers

Trying to Understand this Image function

private void ReadImage() { int i, j; GreyImage = new int[Width, Height]; //[Row,Column] Bitmap image = Obj; BitmapData bitmapData1 = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), …
Callum Linington
  • 14,213
  • 12
  • 75
  • 154
0
votes
1 answer

Native C string list weak reference to C#

I am creating a .NET adapter for the following C API: void GetStringList(const char *** listOut, rsize_t * listCountOut); I want a C# adapter that looks like: [DllImport("api.dll", CallingConvention = CallingConvention.Cdecl)] static extern void…
jws
  • 2,171
  • 19
  • 30
0
votes
3 answers

How to read string from pointer to buffer in C#

How can I read the error string in C# from this C++ dll call? // // PARAMETERS: // objptr // Pointer to class instance. // // pBuffer // Pointer to buffer receiving NULL terminated error message string. // If…
Santiago Corredoira
  • 47,267
  • 10
  • 52
  • 56
0
votes
2 answers

reassigning a pointer in csharp

I am having an issue trying to port the following code from C to C#. not having much success with using the fixed keyword and a ptr that gets reassigned. Could someone tell me how to represent the following code in c#? p = &table[(i =…
Walter Zydhek
  • 293
  • 4
  • 10