我有以下代码:
class Program
{
private unsafe static void SquarePtrParam(int* input)
{
*input *= *input;
}
private static void SquareRefParam(ref int input)
{
input *= input;
}
private unsafe static void Main()
{
int value = 10;
SquarePtrParam(&value);
Console.WriteLine(value);
int value2 = 10;
SquareRefParam(ref value2);
Console.WriteLine(value2);
//output 100, 100
Console.ReadKey();
}
}
Run Code Online (Sandbox Code Playgroud)
将指针和ref关键字作为参数传递给方法有什么区别?
有人能告诉我这UnmanagedMemoryStream堂课的用途吗?
我无法弄清楚这门课程如何以及何时有用?
自从我用C#编程以来,我没有做过任何指示 - 而且很久以前我的C++时代.我以为我应该刷新我的知识,因为这里有另一个问题,我只是在玩它们.我理解他们都没关系,但我无法弄清楚如何将指针的地址写入控制台......
char c = 'c';
char d = 'd';
char e = 'e';
unsafe
{
char* cp = &d;
//How do I write the pointer address to the console?
*cp = 'f';
cp = &e;
//How do I write the pointer address to the console?
*cp = 'g';
cp = &c;
//How do I write the pointer address to the console?
*cp = 'h';
}
Console.WriteLine("c:{0}", c); //should display "c:h";
Console.WriteLine("d:{0}", d); //should display "d:f";
Console.WriteLine("e:{0}", e); //should …Run Code Online (Sandbox Code Playgroud) 据我所知,将方法标记为不安全将禁用对该代码的一些CLR检查,但除了DLL/EXE无法运行之外,这对系统的其他部分是否安全有任何影响.不受信任的环境.
特别是,
我有详重绘在64位Windows嵌套的控制问题,在这里和一个解决方案(即似乎工作的一个)涉及不安全的代码,我想明白,添加此代码有我的项目的效果.
我如何获得不安全类的实例?
我总是得到安全例外.我列出了OpenJDK 6实现的代码.我想把功能sun.misc.Unsafe提供给我,但我总是得到了SecurityException("Unsafe").
public static Unsafe getUnsafe() {
Class cc = sun.reflect.Reflection.getCallerClass(2);
if (cc.getClassLoader() != null)
throw new SecurityException("Unsafe");
return theUnsafe;
}
Run Code Online (Sandbox Code Playgroud)
(请不要试图告诉我使用这门课是多么不安全.)
我遇到了 Rust 借用检查器错误,我认为这是当前非词法生命周期实现的限制。我想写的代码看起来像这样:
struct Thing {
value: i32
}
impl Thing {
fn value(&self) -> &i32 {
&self.value
}
fn increment(&mut self) {
self.value += 1;
}
}
/// Increments the value of `thing` if it is odd, and returns a reference to the value.
fn increment_if_odd(thing: &mut Thing) -> &i32 {
let ref_to_value = thing.value();
if (*ref_to_value % 2) == 0 {
return ref_to_value;
}
thing.increment(); // fails to compile because the immutable borrow `ref_to_value` is still alive …Run Code Online (Sandbox Code Playgroud) 我需要将C#4.0中的一些嵌套结构封装成二进制blob以传递给C++框架.
到目前为止,我使用unsafe/ fixed处理原始类型的固定长度数组已经取得了很大的成功.现在我需要处理一个包含其他结构的嵌套固定长度数组的结构.
我正在使用复杂的变通方法来展平结构,但后来我遇到了一个MarshalAs属性的例子,看起来它可以省去很多问题.
不幸的是,虽然它给了我正确的数据量,它似乎也阻止了fixed阵列被正确编组,正如该程序的输出所示.您可以通过在最后一行放置断点并检查每个指针的内存来确认失败.
using System;
using System.Threading;
using System.Runtime.InteropServices;
namespace MarshalNested
{
public unsafe struct a_struct_test1
{
public fixed sbyte a_string[3];
public fixed sbyte some_data[12];
}
public struct a_struct_test2
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public sbyte[] a_string;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public a_nested[] some_data;
}
public unsafe struct a_struct_test3
{
public fixed sbyte a_string[3];
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public a_nested[] some_data;
}
public unsafe struct a_nested
{
public fixed …Run Code Online (Sandbox Code Playgroud) 我一直在尝试确定在C#中使用fixed语句对于包含固定数组的托管不安全结构的真正成本.请注意我不是指非托管结构.
具体来说,有没有理由避免下面的'MultipleFixed'类显示的模式?简单地修复数据的成本是非零,接近零(==成本类似于在进入/退出固定范围时设置和清除单个标志),还是足够重要以避免在可能的情况下?
显然,这些课程是为了帮助解释这个问题而设计的.这是针对XNA游戏中的高使用率数据结构,其中此数据的读/写性能至关重要,因此如果我需要修复数组并将其传递到任何地方,我会这样做,但如果没有任何差别我我更喜欢将fixed()本地保留在方法中,以帮助保持函数签名对于不支持不安全代码的平台更加轻松.(是的,它的一些额外的咕噜声代码,但不管它需要......)
unsafe struct ByteArray
{
public fixed byte Data[1024];
}
class MultipleFixed
{
unsafe void SetValue(ref ByteArray bytes, int index, byte value)
{
fixed(byte* data = bytes.Data)
{
data[index] = value;
}
}
unsafe bool Validate(ref ByteArray bytes, int index, byte expectedValue)
{
fixed(byte* data = bytes.Data)
{
return data[index] == expectedValue;
}
}
void Test(ref ByteArray bytes)
{
SetValue(ref bytes, 0, 1);
Validate(ref bytes, 0, 1);
}
}
class SingleFixed
{
unsafe void SetValue(byte* data, int index, … 运行此代码时:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace StructLayoutTest
{
class Program
{
unsafe static void Main()
{
Console.WriteLine(IntPtr.Size);
Console.WriteLine();
Sequential s = new Sequential();
s.A = 2;
s.B = 3;
s.Bool = true;
s.Long = 6;
s.C.Int32a = 4;
s.C.Int32b = 5;
int* ptr = (int*)&s;
Console.WriteLine(ptr[0]);
Console.WriteLine(ptr[1]);
Console.WriteLine(ptr[2]);
Console.WriteLine(ptr[3]);
Console.WriteLine(ptr[4]);
Console.WriteLine(ptr[5]);
Console.WriteLine(ptr[6]);
Console.WriteLine(ptr[7]); //NB!
Console.WriteLine("Press any key");
Console.ReadKey();
}
[StructLayout(LayoutKind.Explicit)]
struct Explicit
{
[FieldOffset(0)]
public int Int32a;
[FieldOffset(4)]
public int Int32b;
}
[StructLayout(LayoutKind.Sequential, Pack …Run Code Online (Sandbox Code Playgroud)