unsafe Questions

4

Solved

I want to use c++ code in c# for Unity using CLR. The program works properly outside of unity, but inside of engine it gives me an error: "cs0227: unsafe code requires the 'unsafe' command li...
Calc asked 24/8, 2016 at 19:59

2

Solved

I have multiple threads doing a computation and want to collect the results into a pre-allocated vector. To turn off the borrow checker, I wrote the function: fn set_unsync(vec: &Vec<usize&g...
Negro asked 10/10, 2022 at 20:33

5

Solved

I'm trying to create an array of bytes whose length is UInt32.MaxValue. This array is essentially a small(ish) in-memory database: byte[] countryCodes = new byte[UInt32.MaxValue]; On my machine,...
Linders asked 15/10, 2010 at 16:45

1

I used the bindgen crate to create bindings from Rust to a C library but I get a whole bunch of warnings saying: warning: `extern` block uses type `u128`, which is not FFI-safe = note: 128-bit inte...
Dasie asked 21/8, 2020 at 15:50

8

Solved

Why do I get the following error? Unsafe code may only appear if compiling with /unsafe"? I work in C# and Visual Studio 2008 for programming on Windows CE.
Arise asked 8/1, 2010 at 8:49

2

Solved

The std::cell::Ref struct in Rust is defined as follows: pub struct Ref<'b, T: ?Sized + 'b> { // NB: we use a pointer instead of `&'b T` to avoid `noalias` violations, because a // `Ref...
Gadoid asked 2/12, 2023 at 14:8

3

Solved

Let's say I want to have a value type of 7 bytes (or 3 or 777). I can define it like that: public struct Buffer71 { public byte b0; public byte b1; public byte b2; public byte b3; public byt...
Pish asked 21/5, 2010 at 10:57

1

Solved

I have the following data structure (simplified): use std::collections::HashMap; pub struct StringCache { // the hashmap keys point to elements stored in `storage` // so never outlive this datast...
Necrophobia asked 30/5, 2023 at 12:11

17

I know Java doesn't have pointers, but I heard that Java programs can be created with pointers and that this can be done by the few who are experts in Java. Is it true?
Trask asked 17/11, 2009 at 16:34

2

Solved

With the release of C# 7.2 there is now the ability to have readonly structs, which in many cases can improve performance. For one of my structs I am using a fixed-size byte array to actually hold...
Nacred asked 10/12, 2017 at 6:28

4

Solved

I have a byte buffer of unknown size, and I want to create a local struct variable pointing to the memory of the beginning of the buffer. Following what I'd do in C, I tried a lot of different thin...
Marindamarinduque asked 28/2, 2017 at 2:23

5

Solved

I'm trying to convert a Vec of u32s to a Vec of u8s, preferably in-place and without too much overhead. My current solution relies on unsafe code to re-construct the Vec. Is there a better way to ...
Locomotor asked 6/4, 2018 at 10:13

3

Solved

Is it possible to use Moq to mock an unsafe interface? For example I have (MCVE): [TestClass] public class UnitTest1 { [TestMethod] public unsafe void TestMethod1() { Mock<IMyUnsafeInterfac...
Panpsychist asked 2/9, 2014 at 10:20

3

Solved

Consider the following: import ( "log" "unsafe" ) type Foo struct { Bar int32 } func main() { log.Println(int(unsafe.Sizeof(Foo{}))) } Why is determining the size of a variable considered ...
Hawaiian asked 24/9, 2018 at 14:15

1

Solved

How can old school multi-threading (no wrapping mutex) can be achieve in Rust? And why is it undefined behavior? I have to build a highly concurrent physic simulation. I am supposed to do it in C, ...
Mapp asked 27/1, 2023 at 0:2

5

Solved

I came across a situation where I have a pretty big file that I need to read binary data from. Consequently, I realized that the default BinaryReader implementation in .NET is pretty slow. Upon lo...
Halfway asked 6/8, 2009 at 11:47

2

I have a fixed-size array [T; SIZE] of values of a type T that is ordered (it implements Ord, but not necessarily Clone or Default). I would like to extract the smallest value of the array and drop...
Rotation asked 30/7, 2020 at 9:27

4

Solved

I'm developing for a single-core embedded chip. In C & C++ it's common to statically-define mutable values that can be used globally. The Rust equivalent is roughly this: static mut MY_VALUE: u...
Maggee asked 24/5, 2022 at 20:39

9

Solved

I would like to get a byte[] from a float[] as quickly as possible, without looping through the whole array (via a cast, probably). Unsafe code is fine. Thanks! I am looking for a byte array 4 tim...
Octave asked 6/3, 2009 at 14:32

3

Solved

Is there a way to get the length of a fixed size buffer? Something like: public struct MyStruct { public unsafe fixed byte buffer[100]; public int foo() { return sizeof(buffer); // Compile e...
Aubree asked 9/5, 2016 at 17:7

3

Solved

I have a C# class that contains an int[] array (and a couple of other fields, but the array is the main thing). The code often creates copies of this class and profiling shows that the Array.Copy()...
Assistance asked 23/4, 2014 at 15:27

2

I'm running into a Rust borrow checker error that I believe is a limitation of the current implementation of non-lexical lifetimes. The code I want to write looks something like this: struct Thing ...
Lactalbumin asked 22/10, 2021 at 16:23

4

Solved

Until now I have just taken for granted that Marshal.SizeOf is the right way to compute the memory size of a blittable struct on the unmanaged heap (which seems to be the consensus here on SO and a...
Foretop asked 23/3, 2018 at 23:58

3

Solved

I'm trying to compile a program on the new DNX4.6 core, but it won't compile due to: error CS0227: Unsafe code may only appear if compiling with /unsafe This is my code: [CompilerGenerated] pub...
Marou asked 8/7, 2015 at 14:9

2

Solved

I know using the /unsafe flag in C#, you can use pointers. In C/C++ to delete a pointer you would use free(pointer); and delete pointer; respectively. However, how would you achieve the same effect...
Rammish asked 11/8, 2013 at 14:27

© 2022 - 2024 — McMap. All rights reserved.