奇怪的引用行为(与 int 相同的内存地址)

发布于 2024-10-19 04:04:51 字数 486 浏览 3 评论 0原文

嘿! 我现在正在学习引用类型,但我不明白为什么x与y具有相同的内存地址?他们不应该有不同的地址吗?

class Program
{
    static void Main(string[] args)
    {
        int x = 10; // int -> stack
        int y = x; // assigning the value of num to mun. 
        DisplayMemAddress(x);
        DisplayMemAddress(y);
        Console.ReadLine();
    }
    static unsafe void DisplayMemAddress(int x)
    {
        int* ptr = &x;
        Console.WriteLine("0x" + new IntPtr(ptr).ToString("x"));
    }
}

Hy!
Iam learning refference types now, and I dont get it why does x has the same memory address as y? Shouldn't they have different addresses?

class Program
{
    static void Main(string[] args)
    {
        int x = 10; // int -> stack
        int y = x; // assigning the value of num to mun. 
        DisplayMemAddress(x);
        DisplayMemAddress(y);
        Console.ReadLine();
    }
    static unsafe void DisplayMemAddress(int x)
    {
        int* ptr = &x;
        Console.WriteLine("0x" + new IntPtr(ptr).ToString("x"));
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟凡古楼 2024-10-26 04:04:51

Main 中的 xy 是独立变量。它们可以存储不同的值。他们不能位于同一地址。请注意,它们是值类型变量 - 在这段代码中您实际上根本没有了解引用类型,因为它使用任何引用类型(除了字符串程序控制台)。

但是,您的代码并没有显示这一点 - 它显示的是 DisplayMemAddress 中的参数的地址,这是完全不同的。 xy 的值按值传递到该方法中。如果您将 DisplayMemAddress 方法中的参数重命名为 z,将会很有帮助:

static unsafe void DisplayMemAddress(int z)
{
    int* ptr = &z;
    Console.WriteLine("0x" + new IntPtr(ptr).ToString("x"));
}

现在更容易讨论了。您显示的是 z 的地址,而不是 xy。该地址将位于堆栈上(作为实现细节),并且由于两次调用中堆栈的高度相同,因此它将显示相同的值。现在,如果您将方法更改为使用引用传递,您实际上将看到 xy 的地址:

class Program
{
    static void Main(string[] args)
    {
        int x = 10; // int -> stack
        int y = x; // assigning the value of num to mun. 
        DisplayMemAddress(ref x);
        DisplayMemAddress(ref y);
        Console.ReadLine();
    }

    static unsafe void DisplayMemAddress(ref int z)
    {
        fixed (int* ptr = &z)
        {
            Console.WriteLine("0x" + new IntPtr(ptr).ToString("x"));
        }
    }
}

老实说,显示地址并不是学习引用类型、值类型和参数传递(IMO)的最佳方式。

我有几篇文章您可能会觉得有用:

x and y in Main are independent variables. They can store different values. They can't be at the same address. Note that they're value type variables though - you're not actually learning about reference types at all in this code, as it doesn't use any reference types (except string, Program and Console).

However, your code doesn't show that - it's showing the address of the parameter in DisplayMemAddress, which is entirely different. The values of x and y are passed by value into the method. It would be helpful if you'd rename the parameter in your DisplayMemAddress method to z:

static unsafe void DisplayMemAddress(int z)
{
    int* ptr = &z;
    Console.WriteLine("0x" + new IntPtr(ptr).ToString("x"));
}

Now it's easier to talk about. You're displaying the address of z, not x or y. That address will be on the stack (as an implementation detail) and as the stack is the same height in both calls, it'll show the same value. Now if you change the method to use pass-by-reference, you'll actually see the address of x and y:

class Program
{
    static void Main(string[] args)
    {
        int x = 10; // int -> stack
        int y = x; // assigning the value of num to mun. 
        DisplayMemAddress(ref x);
        DisplayMemAddress(ref y);
        Console.ReadLine();
    }

    static unsafe void DisplayMemAddress(ref int z)
    {
        fixed (int* ptr = &z)
        {
            Console.WriteLine("0x" + new IntPtr(ptr).ToString("x"));
        }
    }
}

To be honest, showing addresses isn't the best way of learning about reference types, value types and parameter passing IMO.

I have a couple of articles you might find useful though:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文