unsafe Questions
2
Solved
A pointer to an array, let's say:
p := uintptr(unsafe.Pointer(&array))
size := 5
I can't access to the variable array, the code above is used to make it more clear.
Also, I know the size of...
3
Solved
Does having a reference &'a T immediately cause UB (undefined behavior) if 'a is larger than the referenced value? OR is it fine to have such a reference as long as it does not outlive the refe...
2
I am looking for a function similar to slice::split_at_mut. Let's name it split_at with the signature
pub fn split_at<T>(v: Vec<T>, mid: usize) -> (Vec<T>, Vec<T>)
such ...
Bradfordbradlee asked 29/6, 2021 at 13:35
1
Solved
I know we can cast from reference -> raw pointer -> address (usize), but can we do it backwards especially with lifetime annotations? For example, I have following code:
struct Name<'a>...
2
I'm doing something with MaybeUninit and FFI in Rust that seems to work, but I suspect may be unsound/relying on undefined behavior.
My aim is to have a struct MoreA extend a struct A, by includin...
Airspeed asked 20/4, 2020 at 8:57
1
I need to write an unsafe method in C# and apparently "unsafe construct cannot be used in safe context". I went to project properties and saw a switch to allow unsafe code in Debug and Release buil...
2
Solved
I'm trying to understand how to get a string from an unsafe byte pointer in the following struct. SDL_TEXTINPUTEVENT_TEXTSIZE is 32.
[StructLayout(LayoutKind.Sequential)]
public unsafe struct SDL_...
3
Solved
How do I get the instance of the unsafe class?
I always get the security exception. I listed the code of the OpenJDK 6 implementation. I would like to mess around with the function sun.misc.Unsafe...
1
Solved
Is there any way to achieve undefined behavior in Rust without using unsafe?
Of course, such behavior can be wrapped by a third-party library in a "safe" function so let's assume we're us...
Heterochromous asked 24/6, 2020 at 16:12
1
Solved
Consider the following:
// Just a sequence of adjacent fields of same the type
#[repr(C)]
#[derive(Debug)]
struct S<T> {
a : T,
b : T,
c : T,
d : T,
}
impl<T : Sized> S<T> {
...
Sudhir asked 7/6, 2020 at 2:15
5
Solved
I want to convert a byte* to a byte[], but I also want to have a reusable function to do this:
public unsafe static T[] Create<T>(T* ptr, int length)
{
T[] array = new T[length];
for (int...
4
Solved
In Java 8 three memory barrier instructions were added to Unsafe class (source):
/**
* Ensures lack of reordering of loads before the fence
* with loads or stores after the fence.
*/
void loadFe...
Hernandez asked 12/5, 2014 at 7:28
2
Solved
I have a struct Test I want to implement std::future::Future that would poll function:
use std::{
future::Future,
pin::Pin,
task::{Context, Poll},
};
struct Test;
impl Test {
async fn functio...
Dysgenic asked 18/4, 2020 at 19:38
1
Solved
I'm working on a library that help transact types that fit in a pointer-size int over FFI boundaries. Suppose I have a struct like this:
use std::mem::{size_of, align_of};
struct PaddingDemo {
d...
Bespangle asked 9/4, 2020 at 4:31
1
Solved
I was wandering in the Restricted Section of the Haskell Library and found these two vile spells:
{- System.IO.Unsafe -}
unsafeDupablePerformIO :: IO a -> a
unsafeDupablePerformIO (IO m) = case ...
Hectograph asked 3/4, 2020 at 22:14
6
Solved
i am using Unity3D 4.3 and calling a DLL that i created.
when trying to call the only function it have which is this:
void GetModelReferences(int &nVertices, float * vertices, int &nTriang...
Cabal asked 1/7, 2014 at 14:30
3
Solved
I am using Visual studio code and when I try to run an unsafe code it throws the following error ""message": Unsafe code may only appear if compiling with /unsafe"
and as in visual studio, it doe...
Hintze asked 1/6, 2018 at 5:36
1
Solved
I'm in a situation where I'm working with data wrapped in an Arc, and I sometimes end up using into_raw to get the raw pointer to the underlying data. My use case also calls for type-erasure, so th...
Torietorii asked 9/1, 2020 at 20:30
1
I am trying to understand the problems with duplicated code for & and &mut in getter-type functions. I'm trying to understand whether a particular solution to this problem using a cast insi...
2
Solved
I have a struct that is not Send because it contains Rc. Lets say that Arc has too big overhead, so I want to keep using Rc. I would still like to occasionally Send this struct between threads, but...
6
I understand unsafe code is more appropriate to access things like the Windows API and do unsafe type castings than to write more performant code, but I would like to ask you if you have ever notic...
Ulaulah asked 21/3, 2011 at 7:18
1
I'm using the following code to make image masks in C#:
for(int x = 0; x < width; x++)
{
for(int y = 0; y < height; y++)
{
bmp.SetPixel(x,y,Color.White);
}
}
for(int x = left; x < wi...
2
Solved
I have a Haskell project that uses fmap on data structures very intensively. In order to avoid traversing the same data structure again and again, while retaining the possibility to use fmap libera...
Emalee asked 13/8, 2019 at 17:53
1
Solved
&mut T and &mut T results in a compilation error; this is great, it's objectively wrong to borrow mutably twice.
Is *mut T and*mut T undefined behaviour or is this a perfectly valid thing ...
Preventer asked 5/8, 2019 at 19:1
1
Solved
In a rather low level part of a project of mine, a function receives a mutable slice of primitive data (&mut [u32] in this case). This data should be written to a writer in little endian.
Now...
© 2022 - 2024 — McMap. All rights reserved.