unsafe

unsafe C#: How can I create an int[] from a pointer to a preexisting memory location?

↘锁芯ラ 提交于 2021-02-19 05:50:08
问题 I am using shared memory for inter-process communication in an unsafe class. Part of the memory is reserved to hold a fixed array of int. Basically, I have a method that sets up the shared memory. Something like this: private int* sizePtr; private ???* arrayPtr; void SetupMemory(byte *pointerToSharedMem) { this.sizePtr = (int*)pointerToSharedMem; pointerToSharedMem += sizeof(int); this.arrayPtr = (???*)pointerToSharedMem; pointerToSharedMem += sizeof(int) * FixedSizeOfArray; } How do I need

Do aliasing mutable raw pointers (*mut T) cause undefined behaviour?

我与影子孤独终老i 提交于 2021-02-18 20:59:58
问题 &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 to do? That is, is mutable pointer aliasing valid? What makes it even worse is that &mut T and *mut T actually compiles and works as intended, I can modify a value through the reference, the pointer, and then the reference again... but I've seen someone say that it's undefined behaviour. Yeah, "someone said

Do aliasing mutable raw pointers (*mut T) cause undefined behaviour?

♀尐吖头ヾ 提交于 2021-02-18 20:59:13
问题 &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 to do? That is, is mutable pointer aliasing valid? What makes it even worse is that &mut T and *mut T actually compiles and works as intended, I can modify a value through the reference, the pointer, and then the reference again... but I've seen someone say that it's undefined behaviour. Yeah, "someone said

C# compile errors in unsafe code

徘徊边缘 提交于 2021-02-11 15:52:08
问题 Compiling a VS 2010 c# project (.NET 4.0, any CPU, allow unsafe code = checked) we are getting a variety of compile errors as below: Operator '*' cannot be applied to operands of type 'System.IntPtr' and 'int' Constant value '325486741' cannot be converted to a 'int' (use 'unchecked' syntax to override) Cannot convert type 'string' to 'char*' Cannot implicitly convert type 'long' to 'byte*'. An explicit conversion exists (are you missing a cast?) Invalid expression term 'ref' All these are

C# compile errors in unsafe code

一曲冷凌霜 提交于 2021-02-11 15:51:09
问题 Compiling a VS 2010 c# project (.NET 4.0, any CPU, allow unsafe code = checked) we are getting a variety of compile errors as below: Operator '*' cannot be applied to operands of type 'System.IntPtr' and 'int' Constant value '325486741' cannot be converted to a 'int' (use 'unchecked' syntax to override) Cannot convert type 'string' to 'char*' Cannot implicitly convert type 'long' to 'byte*'. An explicit conversion exists (are you missing a cast?) Invalid expression term 'ref' All these are

C# compile errors in unsafe code

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-11 15:51:01
问题 Compiling a VS 2010 c# project (.NET 4.0, any CPU, allow unsafe code = checked) we are getting a variety of compile errors as below: Operator '*' cannot be applied to operands of type 'System.IntPtr' and 'int' Constant value '325486741' cannot be converted to a 'int' (use 'unchecked' syntax to override) Cannot convert type 'string' to 'char*' Cannot implicitly convert type 'long' to 'byte*'. An explicit conversion exists (are you missing a cast?) Invalid expression term 'ref' All these are

how to get a direct byte buffer from an address location

旧巷老猫 提交于 2021-02-11 12:32:29
问题 In this opencv example, the Mat object has a nativeObj field, returning a long that represents the address of the object (i.e 140398889556640 ). Because the size of the data within the object is known, I wish to access the contents of the Mat object directly, returning a byte buffer. What is the best way to do so? 回答1: You can wrap the address with a DirectByteBuffer or use Unsafe. While you can do this, you probably shouldn't. I would explore all other options first. // Warning: only do this

Can't use keyword 'fixed' for a variable in C#

半城伤御伤魂 提交于 2021-02-09 05:28:48
问题 I tested the keyword fixed with array and string variables and worked really well but I can't use with a single variable. static void Main() { int value = 12345; unsafe { fixed (int* pValue = &value) { // problem here *pValue = 54321; } } } The line fixed (int* pValue = &value) causes a complier error. I don't get it because the variable value is out of the unsafe block and it is not pinned yet. Why can't I use fixed for the variable value ? 回答1: This is because value is a local variable,

Replacing C# method of declaring type which implements an interface and inherits from base

爱⌒轻易说出口 提交于 2021-02-08 08:38:36
问题 I'm trying to swap out the contents of a method at runtime for the purposes of unit testing legacy code. I've been working with these SO answers; Dynamically replace the contents of a C# method? How to replace the pointer to the overridden (virtual) method in the pointer of my method? (Release x64 and x86) Here's a full code sample of what I have so far. using System; using System.Diagnostics; using System.Reflection; using System.Runtime.CompilerServices; namespace Foo.Bar { public interface

Define multiple classes at runtime using Unsafe.defineClass

守給你的承諾、 提交于 2021-02-08 05:58:39
问题 I am working on a REPL for a custom programming language of mine. It is implemented on top of the compiler, which it uses to generate the bytecode for the input and convert it to a Class<?> instance using the sun.misc.Unsafe.defineClass(String, byte[], int, int, ClassLoader, ProtectionDomain) method. The relevant code looks like this (irrelevant parts like exception handling omitted): void compileAndLoad(List<ICompilable> compilables) { List<Class<?>> classes = ...; for (ICompilable c :