Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
Visual C#
Pointer types
 How to: Use Pointers to Copy an Arr...

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
C# Programming Guide
How to: Use Pointers to Copy an Array of Bytes (C# Programming Guide)

The following example uses pointers to copy bytes from one array to another using pointers.

This example uses the unsafe keyword, which allows pointers to be used within the Copy method. The fixed statement is used to declare pointers to the source and destination arrays. This pins the location of the source and destination arrays in memory so that they will not be moved by garbage collection. These memory blocks will be unpinned when the fixed block completes. Because the Copy function in this example uses the unsafe keyword, it must be compiled with /unsafe compiler option.

Example

C#
// compile with: /unsafe
C#
class TestCopy
{
    // 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 System.ArgumentException();
        }

        int srcLen = src.Length;
        int dstLen = dst.Length;
        if (srcLen - srcIndex < count || dstLen - dstIndex < count)
        {
            throw new System.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 i = 0 ; i < count / 4 ; i++)
            {
                *((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 i = 0; i < count % 4 ; i++)
            {
                *pd = *ps;
                pd++;
                ps++;
            }
        }
    }

    static void Main()
    {
        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);
        System.Console.WriteLine("The first 10 elements are:");

        for (int i = 0; i < 10; ++i) 
        {
            System.Console.Write(b[i] + " ");
        }
        System.Console.WriteLine("\n");
    }
}

Output

The first 10 elements are:
0 1 2 3 4 5 6 7 8 9 

See Also

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
A small bug in this example      Tom Van Holle   |   Edit   |   Show History

This sample illustrates copying the data using fixed pointers. This method of copying is much faster then Buffer.BlockCopy, but requires the /unsafe compiler option. Use it whenever fast copying of buffers is needed. A small code fix is required in this sample to be able to use all options:

The srcIndex and dstIndex parameters are not used in this sample, to enable offset in the source and/or destination buffers add this code within the fixed scope:

//move the pointers to their offset
ps += srcIndex;
pd += dstIndex;

So the code becomes:
...

// 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;

//--> ADD THIS: move the pointers to their offset
ps += srcIndex;
pd += dstIndex;

// Loop over the count in blocks of 4 bytes, copying an integer (4 bytes) at a time:
for (int i = 0; i < count / 4; i++)
...
}

Tags What's this?: bug (x) code (x) contentbug (x) fast (x) fix (x) memory (x) Add a tag
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker