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

C# memory after fixed and the Garbage Collector

I am reading bytes from a binary file with BinaryReader.Read and using the fixed statement to 'convert' the bytes into a struct: [StructLayout(LayoutKind.Sequential, Pack = 1)] unsafe struct MyStruct { // some variables.. too many to list …
Michael Stimson
  • 314
  • 1
  • 4
  • 19
0
votes
2 answers

Android: uses unchecked or unsafe operations

I'm getting this message while building my project: ...\listadapter\MyAdapter.java: uses unchecked or unsafe operations. Recompile with -Xlint:unchecked for details. This happens for this lines of code: @Override protected void…
Web.11
  • 406
  • 2
  • 8
  • 23
0
votes
2 answers

Version of C# StringBuilder to allow for strings larger than 2 billion characters

In C#, 64bit Windows + .NET 4.5 (or later) + enabling gcAllowVeryLargeObjects in the App.config file allows for objects larger than two gigabyte. That's cool, but unfortunately, the maximum number of elements that C# allows in a character array is…
Dan W
  • 3,520
  • 7
  • 42
  • 69
0
votes
1 answer

Safe Indexing Inside Unsafe Code

Good morning, afternoon or night, Foreword: The code below does nothing really useful. It is just for explanation purposes. Is there anything wrong with allocating and using an array "the safe mode" inside unsafe code? For example, should I write my…
Miguel
  • 3,466
  • 8
  • 38
  • 67
0
votes
0 answers

Is there a case where the "unsafe" keyword and the "unsafe" compilation option are NOT used together?

From what I've read the unsafe keyword is used to make a piece of code (method, etc.) as "unsafe", allowing for pointer logic to be used. And if you have that keyword in your code it will not compile, unless you specify the unsafe compilation…
J. Doe
  • 1,147
  • 1
  • 13
  • 21
0
votes
2 answers

is unsafe to copy a three-dimensional array using Arraylist?

I wonder if can I copy a 3-dimensional array, using ArrayList<>() as a middle step. Please, consider the following code: /*variable declaration*/ int[][][] array; int[][][] array2; ArrayList list; …
Esneyder
  • 471
  • 1
  • 5
  • 19
0
votes
1 answer

pointer to 3d array

i have a 3D array and i want to get IntPtr to point on it so here what i did ..can anyone tell me if it's right or not... fixed (Int16* mypointer = &myvolume[0, 0, 0]) { //then i cast mypointer as IntPtr } notice that myvolume is of dimensions…
Sara S.
  • 1,365
  • 1
  • 15
  • 33
0
votes
1 answer

How to restart USB Device without exiting Application

In my application I use USB device for some operation, sometimes happens that the USB disconnected and connected again, after that I can't use the device from my application and to keep using it I need to restart the app, How could I use it without…
Leon Barkan
  • 2,676
  • 2
  • 19
  • 43
0
votes
2 answers

getting the memory location for a java byte array using sun.misc.unsafe

I want to pass the memory location of a byte array to a jni call so that the array does not have to be copied. There is a question here that hints this is possible with sun.misc.Unsafe. Is there an example of how this can be done? The use case is…
zcaudate
  • 13,998
  • 7
  • 64
  • 124
0
votes
3 answers

how to get a direct byte buffer from an address location

In this opencv example, the Mat object has a nativeObj field, returning a long that represents the address of the object (i.e 140398889556640). Because the size of the data within the object is known, I wish to access the contents of the Mat object…
zcaudate
  • 13,998
  • 7
  • 64
  • 124
0
votes
0 answers

Unsafe.copyMemory fails with exception

I am trying to compare the performance of System::arrayCopy vs Unsafe::copyMemory for Object[]. I assume that Unsafe::copyMemory should be faster since it make less checks. However I can't even make it work! Could you pls help me understand what am…
Vadim
  • 576
  • 1
  • 4
  • 13
0
votes
0 answers

How unsafe is passing the DangerousHandle of an AutoResetEvent to a native library?

Context: My situation is that I have to use an externally written native library from a managed .net environment. This native library sends messages realtime to my application at small intervals. It is expected from my application to respond within…
Bart
  • 547
  • 2
  • 12
0
votes
1 answer

When is it safe to use 'unsafe string modifications' in C#?

private const int RESULT_LENGTH = 10; public static unsafe string Encode1(byte[] data) { var result = new string('0', RESULT_LENGTH); // memory allocation fixed (char* c = result) { for (int i = 0; i < RESULT_LENGTH; i++) …
0
votes
1 answer

using $_SERVER['REMOTE_ADDR'] , $_SERVER['HTTP_CLIENT_IP'] & $_SERVER['HTTP_X_FORWARDED_FOR']

i'm coding web application and i wanna secure my customers accounts so i'll get their IPs and store them in the database is that enough to use $_SERVER['REMOTE_ADDR'] OR It's better to use…
JORDAN MI
  • 78
  • 1
  • 8
0
votes
1 answer

I have a C# code. I want to convert it in vb.net. But I am unable to convert unsafe and fixed key word

Unasafe and fixed in vb.net [StructLayout(LayoutKind.Sequential, Pack = 1)] public unsafe struct SGDeviceInfoParam { private const int SGDEV_SN_LEN = 15; // Device Serial Number Length public UInt32 DeviceID;…