MemCpy??? Please assist???

Hello - I have written a routine in C# that is supposed to replace the MemCpy C++ routine - but the numbers I get are nothing like that displayed in the array below - for the given argument to MemCopy, how is it arriving at the values in elements 0, 1, and 2, below??? Thank you in advance.

memcpy((POINTER)aDataStructure->unsignedLongInt16ElementArray, (POINTER)a20CharacterBufferHoldingTheHome123Value, countOfTen )

Arg1: unsignedLongInt16ElementArray = See below
Arg2: a20CharacterBufferHoldingTheHome123Value = TheHome123
Arg3: countOfTen = 10

Result:
>usignedLongInt16ElementArray[ 0] = 1214605396
>usignedLongInt16ElementArray[ 1] = 828730735
>usignedLongInt16ElementArray[ 2] = 13106
>usignedLongInt16ElementArray[ 3] = 0
>usignedLongInt16ElementArray[ 4] = 0
>usignedLongInt16ElementArray[ 5] = 0
>usignedLongInt16ElementArray[ 6] = 0
>usignedLongInt16ElementArray[ 7] = 0
>usignedLongInt16ElementArray[ 8] = 0
>usignedLongInt16ElementArray[ 9] = 0
>usignedLongInt16ElementArray[10] = 0
>usignedLongInt16ElementArray[11] = 0
>usignedLongInt16ElementArray[12] = 0
>usignedLongInt16ElementArray[13] = 0
>usignedLongInt16ElementArray[14] = 0
>usignedLongInt16ElementArray[15] = 0
>usignedLongInt16ElementArray[16] = 0
>usignedLongInt16ElementArray[17] = 0
>usignedLongInt16ElementArray[18] = 0
>usignedLongInt16ElementArray[19] = 0
How could you mix C# and C++?

memcpy does not use the number of values but the size in bytes of the memory to copy.
I.e. countOfTen * sizeof(*(aDataStructure->unsignedLongInt16ElementArray))
wow . such a way to name variables. I have to say way too long.
LOL. The naming was to remove any ambiguity for the person assisting me.
You can test the correctness of your memcpy with something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.Runtime.InteropServices;
using System.Linq;
using System.Diagnostics;

class name_space
{
    static unsafe void my_memcpy( ulong[] srce, ulong[] dest )
    {
        int nbytes = Math.Min(srce.Length, dest.Length) * sizeof(ulong);
        fixed( ulong* from = srce, to = dest )
        {
            byte* srce_bytes = (byte*)from;
            byte* dest_bytes = (byte*)to;
            for (int i = 0; i < nbytes; ++i) dest_bytes[i] = srce_bytes[i];
        }
    }

    [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "memcpy") ]
    static extern UIntPtr libc_memcpy(UIntPtr dest, UIntPtr src, UIntPtr count);

    static void Main(string[] args)
    {
        ulong[] a = { 1, 2, 3, 4, 5 };
        ulong[] b = new ulong[a.Length];
        ulong[] c = new ulong[a.Length];
        ulong[] d = new ulong[a.Length];

        my_memcpy(a,b);

        a.CopyTo(c,0);

        unsafe
        {
            fixed (void* dest = d, srce = a )
            { libc_memcpy((UIntPtr)dest, (UIntPtr)srce, (UIntPtr)(a.Length * sizeof(ulong))); }
        }

        var a_eq_b = Enumerable.SequenceEqual(a, b); // Linq
        var a_eq_c = Enumerable.SequenceEqual(a, c);
        var a_eq_d = Enumerable.SequenceEqual(a, d);

        Debug.Assert(a_eq_b && a_eq_c && a_eq_d);
        Console.Write("all four are equal? " + (a_eq_b && a_eq_c && a_eq_d).ToString() + '\n');
    }
}

Last edited on
@Coder777 Thank you. (Not mixing. :-))

@JL Borges Above-and-beyond. Thank you!
Topic archived. No new replies allowed.