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
-1
votes
1 answer

Trying to learn and understand the behaviour of unsafe rust within thread execution. Can someone explain what I might done wrong here?

use std::sync::Arc; use std::sync::atomic::{AtomicPtr, Ordering}; use std::thread; fn main() { let mut arr = vec![1,2,3]; let ptr = &mut arr as *mut Vec; println!("ptr : {:?}",ptr); // unsafe { (*ptr)[0] = 2; } let…
-1
votes
2 answers

Rust. UnsafeCell thread safety

I'm using the object from a crate, which was not written by me and i have a struct which i would like to use as a global variable, but i get this error: error[E0277]: `Rc>>` cannot be sent between threads safely …
swapgs
  • 3
  • 2
-1
votes
1 answer

C# How do you monitor accesses to array elements?

Arrays are all of type Array, instead of their underlying type, meaning making arrays out of your own custom primitives with event handling and stuff is useless. Someone on Discord said that it'll probably take either reflection or unsafe…
-1
votes
2 answers

C# - Unsafe and Pointers Basics

I am a C++/Java amateur but completely new to C#. My goal is to make a Tree where Nodes are pointers instead of entire objects that are constantly and slowly copied. That way I can simply pass a memory address instead of copying entire objects byte…
Stev
  • 65
  • 1
  • 7
-1
votes
1 answer

Copying data to transient vertex buffer in bgfx causing SIGSEGV

I'm trying to port an bgfx application I wrote in C++ to Rust using bgfx-rs. Everything works great, until I try to copy some vertex data from the application to an transient vertex buffer. let vertices: [f32; 16] = [ 0.0f32, 0.0f32, …
Constantin
  • 8,721
  • 13
  • 75
  • 126
-1
votes
1 answer

Best way to extract strings from binary data in golang using unsafe

I have an application which loads a byte array of several gigabytes. I dont have control of the binary format. The program spends most of its time converting sections of the array into strings, doing string manipulation and then releasing all of the…
Jay
  • 19,649
  • 38
  • 121
  • 184
-1
votes
1 answer

"The type 'struct' must be a non-nullable value type" error when passing struct with fixed sized strings

I am using a code snippet posted here: Casting a byte array to a managed structure public static class Serializer { public static unsafe byte[] Serialize(T value) where T : unmanaged { byte[] buffer = new byte[sizeof(T)]; …
Rithesh
  • 25
  • 1
  • 6
-1
votes
2 answers

Rust: Safe single-threaded TCP Server

is it possible in pure rust to write a single-threaded TCP Server? In C I would use the select syscall to "listen" to multiple sockets. I only find solutions where people use unsafe to use epoll/select, but I really want to avoid this. I think this…
Alai
  • 123
  • 1
  • 8
-1
votes
1 answer

Can safety be guaranteed when storing 2 copies of a raw pointer created with Box::into_raw() in rust?

I'd like to implement a data structure that stores values in 2 different orders. Values are inserted once (in both orders) and cannot be removed without dropping the entire data structure ("append only"). They can still be mutated in place. The…
Yaar Hever
  • 81
  • 1
  • 6
-1
votes
1 answer

Does unsafe code go out of bounds when incrementing a pointer to an array while iterating through the array?

I am working with image and video manipulation and in a few cases really do need unsafe code for speed purposes. Sometimes I use code something like the following: var data = new byte[n]; fixed (byte* fixData = data) { byte* ptrData =…
MinosIllyrien
  • 330
  • 1
  • 9
-1
votes
1 answer

Can I safely cast Box to Box?

Send is a marker trait, and does not have any impact on memory layout. That said, the mechanics of the Any type remain slightly mysterious to me. Is the following code sound and does it do what I want: the cast type will always downcast correctly to…
cmyr
  • 2,235
  • 21
  • 30
-1
votes
2 answers

Get bytes from float array without allocations (i.e. via cast)

I have a quite large float array (usually >40 million entries) which takes up between 150MB und 250MB megabytes in memory and I need to pass it to two different APIs. Both are thrid party tools which I can't change. One of which only accepts byte[]…
C. Göbeler
  • 65
  • 1
  • 8
-1
votes
1 answer

How to avoid dotfiles or dot directories in a http response

How can I avoid sending files in a dot folder like .git or dot file like .gitignore in a http response? I want to send a error if user request this kind of files.
deepnum
  • 21
  • 1
-1
votes
1 answer

How to change array address in c# unsafe

I have an array of structs that i need to change into an array of ints, without having to manually copy it. I tried getting the array address but without results. This is the equivalent C++ of what i'm trying to do: struct MyStruct { public int…
bbQsauce
  • 1
  • 2
-1
votes
1 answer

How to read a value back from the memory location in C#?

I am writing a simple C# code, wherein I am making use of Pointer to read a value from the memory location. The code works fine when I try to read the value from the same method where I insert the value. However, it does not work when I try to…