Click here to Skip to main content
15,897,334 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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");
    }
}


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
Posted
Updated 26-Jun-16 17:23pm

1 solution

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
 
Share this answer
 
v3
Comments
sarath para 27-Jun-16 13:05pm    
Hi sergey
The same is not happening with ps?
Only pd is showing zero
Sergey Alexandrovich Kryukov 27-Jun-16 13:50pm    
Didn't I answer in full already? Just read it carefully, understand, and see what's going on under the debugger. Are you going to accept my answer formally?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900