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
-2
votes
2 answers

Change the memory address of an object in C#

Is it possible to change the memory address of an object in C# ? Example of what I want: unsafe public Program { public struct Foo { int a; int b; } static void Main(string[] args){ Foo foo= new Foo(); …
-2
votes
1 answer

negative cannot be converted to a uint

I was looking to convert some old code I found to a .net standard compliant version. However I haven't used code like this before so I'm a bit lost on how to use it as it doesn't compile. I understand that uint cannot be negative but how did this…
danewfie
  • 13
  • 3
-2
votes
1 answer

Go pointer multi-thread read and write errors

Normal should be constant output test1 test2 ........ But only test1 output, the program hung up, there is no response The assignment of the pointer is the most basic operation, this should be thread-safe to meet the period But this test has not…
san yue
  • 11
  • 1
-2
votes
1 answer

How do you pass an array-of-arrays to a function in C#?

Note, this question is specifically about an "array-of-arrays", not multi-dimensional array, not jagged array, but a fixed size, square array. In C++ this would be a "plain old array", but this question is not about interoperation, it is just plain…
TamaMcGlinn
  • 2,840
  • 23
  • 34
-2
votes
1 answer

how to deserialize an instance of "Serialization" without the implementation class on the classpath?

I'm writing a tool that need to get an instance of java.io.Serializable from a byte array. The difficulty is that the "real" class is not (and cannot be...) on the classpath (I will not explain why here..). The code below fails on is.readObject()…
titou10
  • 2,814
  • 1
  • 19
  • 42
-2
votes
3 answers

Error 1 Cannot implicitly convert type 'int**' to 'int*'. An explicit conversion exists (are you missing a cast?)

I'm learning C and C# and this question is for C#. I looking at pointers at msdn and this code is not compiling, it gives the error:Error 1 Cannot implicitly convert type int** to int*. An explicit conversion exists (are you missing a cast?).…
somethingSomething
  • 830
  • 7
  • 20
  • 43
-3
votes
1 answer

Unsafe mutating from an immutable context fails under release mode

In the code below, I use unsafe code to convert an immutable reference into a mutable pointer and then I try to edit the inner value by means of this mutable pointer. fn main() { #[repr(transparent)] struct Item(isize); impl Item { …
Isaac Dzikum
  • 184
  • 9
-3
votes
1 answer

In c#, how can I manually move the memory that a reference type points to, into a pinned, fixed, contiguous array or buffer?

I need a buffer of memory which holds actual objects, not references, in memory. I am using Unity Engine for my app. In order to submit a draw-call in Unity, one must provide a Mesh-object and Material-object in the function call. Here is the part…
Tree3708
  • 33
  • 4
-3
votes
1 answer

Pointer reset once method called second time

Can someone explain why the head and tail is reset to null once second IsAMatch is call even though id pass new pointers to it, the old ones get reset. I have equivalent script on c++ and it works perfectly. Any ideas ? my code small code example…
IronHide
  • 327
  • 1
  • 3
  • 17
-3
votes
1 answer

sun.misc.Unsafe putInt, getInt source

Please, tell me, where i can get source of methods sun.misc.Unsafe#getInt(java.lang.Object, long) and sun.misc.Unsafe#putInt(java.lang.Object, long, int). Or how replacement with using pure java types?
wertklop
  • 3
  • 5
-3
votes
1 answer

Array of pointers to arrays in C#

I want to know if it is possible to make an array of pointers to some arrays in C#. I want to do it to overcome the problem of getting OutOfMemoryException while allocating one very large object. I know that I can do it in C like here: Array of…
lukaszg
  • 23
  • 5
-4
votes
1 answer

unsafe code explanation

private static Complex[,] FromBitmapData(BitmapData _BitmapData) { if (_BitmapData.PixelFormat != PixelFormat.Format8bppIndexed) { throw new Exception("Source can be grayscale (8bpp indexed)…
user366312
  • 16,949
  • 65
  • 235
  • 452
-4
votes
1 answer

Reasons for using unsafe code

What are some reasons for using unsafe code? Direct access to the GC and better performance The ability to throw types not-inherited from Exception, better performance, and the ability to write inline IL The ability to write inline IL and…
wonderful world
  • 10,969
  • 20
  • 97
  • 194
-5
votes
1 answer

How to resolve errors with unsafe pointer in C#?

My Code are as follows: public static unsafe bool ExistInURL(params object[] ob) { void* voidPtr = (void*) stackalloc byte[5];//Invalid expression term *((int*) voidPtr) = 0; while (*(((int*) voidPtr)) < ob.Length) …
Meysam Savameri
  • 347
  • 2
  • 4
  • 8
-6
votes
2 answers

Why compiler force me to specify unsafe in c#

I know I've to compile with Unsafe Option to use unsafe code in C# Consider the following static void foo() { int a = 5; int* p = &a; } the above code won't compile unless I mark method as unsafe or wrap above statements inside unsafe…
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
1 2 3
65
66