Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C语言中的不安全结构_C#_Pointers_Struct_Unsafe - Fatal编程技术网

C# C语言中的不安全结构

C# C语言中的不安全结构,c#,pointers,struct,unsafe,C#,Pointers,Struct,Unsafe,我想通过创建简单的结构向量粒子来尝试c的不安全“特性” 情况 我有这两个结构,想把位置和速度向量注入到我的粒子结构中。 作为测试,我想打印出位置的X值,但不知怎么的,我得到了随机值 我这里有以下代码 载体 粒子 节目 问题 它打印的是一个随机值,而不是10。您无法使用正确的参考值 创建一个类似int posX=10的变量 您可以使用变量获取引用。获取编译时引用并读取运行时引用 不要在没有固定指针的情况下使用指针。C栈的性能非常好。你不需要这个 指针通常与链接C/Cpp动态库链接等一起使用。如果您

我想通过创建简单的结构向量粒子来尝试c的不安全“特性”

情况 我有这两个结构,想把位置和速度向量注入到我的粒子结构中。 作为测试,我想打印出位置的X值,但不知怎么的,我得到了随机值

我这里有以下代码

载体

粒子

节目

问题
它打印的是一个随机值,而不是10。

您无法使用正确的参考值

创建一个类似int posX=10的变量

您可以使用变量获取引用。获取编译时引用并读取运行时引用

不要在没有固定指针的情况下使用指针。C栈的性能非常好。你不需要这个


指针通常与链接C/Cpp动态库链接等一起使用。如果您有大于等于30字节的大型结构,则可以使用ref参数标记。

您不能使用正确的ref值

class Program {
    static void Main (string [ ] args) {
        unsafe {
            Vector pos = new Vector(10, 5);
            Particle obj = new Particle(&pos);
            // &pos is at position 0xabcdef00 here.
            // obj.mPosition has a different value here. It points to a different address? Or am I misunderstanding something
            Console.WriteLine(obj.GetPosX( ));
        }
    }
}

public struct Vector {
    public int X;
    public int Y;

    public Vector (int x, int y) {
        X = x;
        Y = y;
    }
}

public unsafe struct Particle {
    private Vector* mPosition;

    public Particle (Vector *position) {
        mPosition = position; // here is x 10
    }

    public int GetPosX ( ) {
        return mPosition->X; // still 10 here
    }
}
创建一个类似int posX=10的变量

您可以使用变量获取引用。获取编译时引用并读取运行时引用

不要在没有固定指针的情况下使用指针。C栈的性能非常好。你不需要这个

指针通常与链接C/Cpp动态库链接等一起使用。如果您有30字节或更大的大型结构,则可以使用ref参数标记

class Program {
    static void Main (string [ ] args) {
        unsafe {
            Vector pos = new Vector(10, 5);
            Particle obj = new Particle(&pos);
            // &pos is at position 0xabcdef00 here.
            // obj.mPosition has a different value here. It points to a different address? Or am I misunderstanding something
            Console.WriteLine(obj.GetPosX( ));
        }
    }
}

public struct Vector {
    public int X;
    public int Y;

    public Vector (int x, int y) {
        X = x;
        Y = y;
    }
}

public unsafe struct Particle {
    private Vector* mPosition;

    public Particle (Vector *position) {
        mPosition = position; // here is x 10
    }

    public int GetPosX ( ) {
        return mPosition->X; // still 10 here
    }
}
这对我有用。 请别问我为什么。你会注意到我没有改变那么多。只是用*pos而不是pos调用Particle。出于某种原因,解决了这个问题。您必须使用unsafe包装代码,然后更改Particle的构造函数

我可以推测它为什么会起作用,但我不想这样。可能由于某种原因,当您将pos作为参数传递时,指针会发生变化

这对我有用。 请别问我为什么。你会注意到我没有改变那么多。只是用*pos而不是pos调用Particle。出于某种原因,解决了这个问题。您必须使用unsafe包装代码,然后更改Particle的构造函数


我可以推测它为什么会起作用,但我不想这样。当您将pos作为参数传递时,指针可能会因为某种原因而改变?

尝试将结构声明为不安全。@RobertHarvey没有帮助。好的,不安全不会创建整个C环境。每次我使用不安全软件时,我都必须固定一些内存以使其正常工作。有关示例,请参见。请注意关键字fixed的用法。这里的另一个示例:看看这里:尝试将结构声明为不安全。@RobertHarvey没有帮助。好的,不安全不会创建整个C环境。每次我使用不安全软件时,我都必须固定一些内存以使其正常工作。有关示例,请参见。注意关键字fixed的用法。这里的另一个例子是:看看这里:谢谢你的帮助。建议的解决方案不是超净的,但很有效。谢谢你的帮助。建议的解决方案不是超级干净的,但它是有效的。是的,这有点奇怪,但是我会选择第一个解决方案,因为它允许我在安全的环境中使用粒子。@Hrant我想知道下面的解决方案是否可以代替主公共int GetPosX{safe{return mPosition->X;}中的不安全解决方案。{safe{return mPosition X;}是的,这有点奇怪,但是,我会选择第一种解决方案,因为它允许我在安全上下文中使用我的粒子。@Hrant我想知道下面的解决方案是否替代了主公共int GetPosX{safe{return mPosition->X;}中的不安全
public class Program
{
    private static void Main(string[] args)
    {
        var pos = new Vector(10, 5);
        var obj = new Particle(pos);

        Console.WriteLine(obj.GetPosX()); // prints random value
    }
}
class Program {
    static void Main (string [ ] args) {
        unsafe {
            Vector pos = new Vector(10, 5);
            Particle obj = new Particle(&pos);
            // &pos is at position 0xabcdef00 here.
            // obj.mPosition has a different value here. It points to a different address? Or am I misunderstanding something
            Console.WriteLine(obj.GetPosX( ));
        }
    }
}

public struct Vector {
    public int X;
    public int Y;

    public Vector (int x, int y) {
        X = x;
        Y = y;
    }
}

public unsafe struct Particle {
    private Vector* mPosition;

    public Particle (Vector *position) {
        mPosition = position; // here is x 10
    }

    public int GetPosX ( ) {
        return mPosition->X; // still 10 here
    }
}