为什么*((int *)pd)一旦pd + = 4就重置为零? [英] Why *((int*)pd) is reseeting to zero once pd+=4 ?

查看:75
本文介绍了为什么*((int *)pd)一旦pd + = 4就重置为零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Test
{
    // The unsafe keyword allows pointers to be used within
    // the following method:
    static unsafe void Copy(byte[] src, int srcIndex,
        byte[] dst, int dstIndex, int count)
    {
        if (src == null || srcIndex < 0 ||
            dst == null || dstIndex < 0 || count < 0)
        {
            throw new ArgumentException();
        }
        int srcLen = src.Length;
        int dstLen = dst.Length;
        if (srcLen - srcIndex < count ||
            dstLen - dstIndex < count)
        {
            throw new ArgumentException();
        }
 
 
            // The following fixed statement pins the location of
            // the src and dst objects in memory so that they will
            // not be moved by garbage collection.          
            fixed (byte* pSrc = src, pDst = dst)
            {
                  byte* ps = pSrc;
                  byte* pd = pDst;

            // Loop over the count in blocks of 4 bytes, copying an
            // integer (4 bytes) at a time:
            for (int n =0 ; n < count/4 ; n++)
            {
                *((int*)pd) = *((int*)ps);
                pd += 4;
                ps += 4;
            }
 
            // Complete the copy by moving any bytes that weren't
            // moved in blocks of 4:
            for (int n =0; n < count%4; n++)
            {
                *pd = *ps;
                pd++;
                ps++;
            }
            }
    }
 
 
    static void Main(string[] args) 
    {
        byte[] a = new byte[100];
        byte[] b = new byte[100];
        for(int i=0; i<100; ++i) 
           a[i] = (byte)i;
        Copy(a, 0, b, 0, 100);
        Console.WriteLine("The first 10 elements are:");
        for(int i=0; i<10; ++i) 
           Console.Write(b[i] + " ");
        Console.WriteLine("\n");
    }
}





我的尝试:



我试图调试但不明白为什么*((int *)pd)重置为零

从他的链接下面获得

https://msdn.microsoft.com/en-us/library/aa288474(v=vs.71).aspx



What I have tried:

I tried to debug but did not understand why *((int*)pd) is resetting to zero
got fromt he below link
https://msdn.microsoft.com/en-us/library/aa288474(v=vs.71).aspx

推荐答案

没有什么是重置 。



在此表达式中,不会修改任何内存部分。根据你的声明, pd 是一个指向byte的指针;所以 pd + = 4 给你另一个向前移动4个字节的指针。它恰好发生了另外4个字节( int 的大小),类型转换指针指向的内存,它用零填充。使用调试器,你会看到。



顺便说一下,在你的代码中,我看不到使用 unsafe 和指针。



-SA
Nothing is "resetting".

In this expression, no part of memory is modified. According to your declaration, pd is a pointer to byte; so pd += 4 gives you another pointer which is shifted forward by 4 bytes. It just so happened that another 4 bytes (size of int), the memory where the type-casted pointer points, it filled with zeros. Use the debugger and you will see.

By the way, in your code I cannot see real justification for using unsafe and pointers.

—SA


这篇关于为什么*((int *)pd)一旦pd + = 4就重置为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆