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

Unsafe C# - Instantiate Pointer at lowest memory address and overwrite

Is it possible to instantiate a pointer using (unsafe) C# at any given memory address (which one would be the lowest?) and simply start overwriting the memory with continuous random data until a PC crashes? Could someone provide me with an example?
Alex
  • 75,813
  • 86
  • 255
  • 348
0
votes
2 answers

Byte array image crop performance issue

Have two pointers (byte*) to 1. B8G8R8A8 pixel data 2. byte buffer to put cropped pixel data, very simple issue. Here is my implementation: private unsafe void Crop(byte* src, byte* dst, Rectangle rSrc, Rectangle rDst) { int sSrc = 4 *…
0
votes
1 answer

CLR on unsafe code and pointer point to array?

how do CLR interact with unsafe code I found various result on Google but i couldn't understand. I am also confused that is Garbage collector work on unsafe code? if yes than how? I cant point pointer to Array,s first element I try this…
Govind Malviya
  • 13,627
  • 17
  • 68
  • 94
0
votes
1 answer

Fast unsafe access for two dimension array

I have a huge two dimension array. I need to get elements from it, but i have a problem - it's EXTREMLY slow (i think a problem in checking indexes from compiler). How i can get elements of my array in unsafe? private byte[][] MY_TASTE_ARRAY; for…
0
votes
1 answer

Is this php function safe

This is not my full code. I am just posting the relevant parts. In my current application the user does not have access to manipulate the variables so it is safe but in future I may write something where they can so while I am thinking about it I…
0
votes
2 answers

Use of unassigned local variable when taking the address of variable

Why in this code compiler does not show error Use of unassigned local variable when taking the address of that variable? int i; int* p = &i; // Use of unassigned local variable i ? int j = *p; // j = 0 j = i; // both valid This will compile…
M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118
0
votes
2 answers

How can classes be passed by reference in .NET?

As we all know, classes in .NET are passed by reference and structs by value. In unsafe code, you can have pointers to structs, but not to classes since they are moved by the GC. When you have a pointer to an array, for example, you either have to…
James Ko
  • 32,215
  • 30
  • 128
  • 239
0
votes
0 answers

Source path "sun\misc\Unsafe.java" was not found.

I'm running a Java code and have loaded JDK 1.8 (the latest) and another library (windy1/google-places-api-java from GitHub to connect with GooglePlaces). The problem is that the program stops running after the first iteration of a "for" (the first…
0
votes
1 answer

Cast non-pointer to pointer type in fixed expression

I'm a bit new to the unsafe side of C# so forgive me if I'm missing something obvious here. I'm looking through some code using .NET Reflector to understand some implementation of the Oculus Rift implementation into C#, but I'm getting a bunch of…
jsmars
  • 1,640
  • 5
  • 21
  • 33
0
votes
1 answer

Fast XOR Algorithm

Is there a way to improve the speed for a XOR-Encryption Algorithm somehow by using some tricks (unsafe code or so)? My current algorithm I am using is the following: public byte[] XOR(byte[] strng, byte[] key) { int string_len = strng.Length; …
yq8
  • 145
  • 1
  • 10
0
votes
1 answer

How to concatenate and hash a username and password (stored in a secure string) in unsafe code

I'm trying to persist whether username and password combination were valid last time a program executed, but without storing the username and password themselves. The goal isn't validation, just to prevent needless attempts to use invalid…
tobriand
  • 1,095
  • 15
  • 29
0
votes
2 answers

Dump a process memory to file / recreate process from dump file

Just curious, maybe someone knows a way: Is it possible, while having an opened process (app domain), dump its entire memory space to a file, send it by wire to a LAN workstation and recreate the process as it was on the first…
itvist
  • 33
  • 1
  • 3
0
votes
1 answer

Java Unsafe Memory Manipulation vs. Java vs. C

are there any benchmarks available for comparing memory access to off heap memory in Java Unsafe vs. Java vs. C regions? For example the Java LZ4 (or L4Z?) compression library stated that their Java port using Unsafe is 66% of the speed of the…
Martin Kersten
  • 5,127
  • 8
  • 46
  • 77
0
votes
1 answer

How to nicely delete unmanaged memory in C#?

I was reading for this question: Difference between destructor, dispose and finalize method I've read that the destructor was used to delete unmanaged resources, but after running a little test : using System; namespace Tests { class…
Sylafrs
  • 1
  • 1
  • 2
0
votes
1 answer

Smooth Filter with 5x5 Convolution Matrix read or write protected memory

So I try to make a smooth filter using C# with unsafe code public static bool Conv5x5(Bitmap b, double[,] m, int factor, int offset) { if (0 == factor) return false; Bitmap bSrc = (Bitmap)b.Clone(); …
petar19992
  • 21
  • 3