PC Review
Forums
Newsgroups
Microsoft DotNet
Microsoft C# .NET
Re: Array.Copy like memmove?
Forums
Newsgroups
Microsoft DotNet
Microsoft C# .NET
Re: Array.Copy like memmove?
![]() |
Re: Array.Copy like memmove? |
|
|
Thread Tools | Rate Thread |
|
|
#1 |
|
Guest
Posts: n/a
|
Hi Daniel,
Why not try it and see? - Best way to learn for the small stuff. Happy experimenting, Fergus |
|
|
|
#2 |
|
Guest
Posts: n/a
|
If I can't get an answer, I'll certainly do some simple
experiments. However, documentation should clearly state what a function does. I need to know for sure, not just based on my experiments. What if I miss a special case? My question: Is Array.Copy designed to be like memmove, or like memcpy? Does anyone know for sure? "Fergus Cooney" <wollus@tesco.net> wrote in message news:<O3UIqgcaDHA.2632@TK2MSFTNGP12.phx.gbl>... > Hi Daniel, > > Why not try it and see? - Best way to learn for the small stuff. > > Happy experimenting, > Fergus Is Array.Copy() like memmove or memcpy? C# in a nutshell says: "The Copy() method works like the C function memmove: it copies a portion of an array to a different postion in the current array (or to a different array)". .NET docs say: "Copies a range of elements from an Array starting at the first element and pastes them into another Array" *** ANOTHER *** I saw this posted before, but no clear answer. Daniel Goldman |
|
|
|
#3 |
|
Guest
Posts: n/a
|
Hi again Daniel,
Help Topic: Array.Copy Method (Array, Array, Int32) ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemarrayclasscopytopic1.htm The documentation says: If sourceArray and destinationArray overlap, this method behaves as if the original values of sourceArray were preserved in a temporary location before destinationArray is overwritten. This following is shown if you select C++ or All in the Language Filter at the top of the page: [C++] This method is equivalent to the standard C/C++ function memmove, not memcpy. Regards, Fergus |
|
|
|
#4 |
|
Guest
Posts: n/a
|
Hi Fergus,
Thanks for finding the answer! The documentation for Array.Copy Method for C# did not have the information, but you dug deeper. Array.Copy is like memmove Daniel Goldman "Fergus Cooney" <wollus@tesco.net> wrote in message news:<eoPk2KzaDHA.2932@tk2msftngp13.phx.gbl>... > Hi again Daniel, > > Help Topic: Array.Copy Method (Array, Array, Int32) > ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemarrayclasscopytopic1.htm > > The documentation says: > > If sourceArray and destinationArray overlap, this method behaves as if > the original values of sourceArray were preserved in a temporary location > before destinationArray is overwritten. > > This following is shown if you select C++ or All in the Language Filter at > the top of the page: > > [C++] This method is equivalent to the standard C/C++ function memmove, > not memcpy. > > Regards, > Fergus |
|
|
|
#5 |
|
Guest
Posts: n/a
|
Daniel Goldman <hhh12347@yahoo.com> wrote:
> Thanks for finding the answer! The documentation for > Array.Copy Method for C# did not have the information, > but you dug deeper. The docuemtation for Array.Copy certainly *does* have the information about what happens if the arrays overlap. It may not say it's equivalent to the C++ memmove without you selecting C++/All, but the relevant information is there. -- Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet/ If replying to the group, please do not mail me too |
|
|
|
#6 |
|
Guest
Posts: n/a
|
Hi Daniel, Jon,
We're talking that strictly? The info for C# doesn't have it, but the info for C++ does. So you're both right. Regards Fergus |
|
|
|
#7 |
|
Guest
Posts: n/a
|
Fergus Cooney <wollus@tesco.net> wrote:
> We're talking that strictly? > > The info for C# doesn't have it, but the info for C++ does. The info for C# *does* say what happens when the arrays overlap. I've got it right here in front of me, with the C# filter applied: <quote> If sourceArray and destinationArray overlap, this method behaves as if the original values of sourceArray were preserved in a temporary location before destinationArray is overwritten. </quote> That's looking on the form which takes (Array, Array, Int32) as its parameters. It's the third paragraph in the remarks. There's no need to switch to the C++ filter in order to understand what the semantics are. -- Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet/ If replying to the group, please do not mail me too |
|
|
|
#8 |
|
Guest
Posts: n/a
|
Hi Jon,
Lol, I wondered if you'd spot that. :-) The thing is, Daniel wanted it spelled out <directly> using whichever of "memmove" or "memcopy" was correct. In other words, putting it in the terms of his question. I quoted the same section in my own post. But Daniel picked up on the bit that was important to him. So you're <still> both right. :-) Regards, Fergus |
|
|
|
#9 |
|
Guest
Posts: n/a
|
As I said, the documentation for Array.Copy did not have the
answer. It repeatedly says "another array", and leaves out "same array", which is misleading. It has a lot of text, does not have either of the key points Fergus found in other pages. Once again, Fergus, thanks for finding the answer. ******************************************* .NET Framework Class Library Array.Copy Method [C#]See Also Array Class | Array Members | System Namespace | Managed Extensions for C++ Programming Language C# C++ JScript Visual Basic Show All Copies a section of one Array to another Array and performs type casting and boxing as required. Overload List Copies a range of elements from an Array starting at the first element and pastes them into another Array starting at the first element. The length is specified as a 32-bit integer. [Visual Basic] Overloads Public Shared Sub Copy(Array, Array, Integer) [C#] public static void Copy(Array, Array, int); [C++] public: static void Copy(Array*, Array*, int); [JScript] public static function Copy(Array, Array, int); Copies a range of elements from an Array starting at the first element and pastes them into another Array starting at the first element. The length is specified as a 64-bit integer. [Visual Basic] Overloads Public Shared Sub Copy(Array, Array, Long) [C#] public static void Copy(Array, Array, long); [C++] public: static void Copy(Array*, Array*, __int64); [JScript] public static function Copy(Array, Array, long); Copies a range of elements from an Array starting at the specified source index and pastes them to another Array starting at the specified destination index. The length and the indexes are specified as 32-bit integers. Supported by the .NET Compact Framework. [Visual Basic] Overloads Public Shared Sub Copy(Array, Integer, Array, Integer, Integer) [C#] public static void Copy(Array, int, Array, int, int); [C++] public: static void Copy(Array*, int, Array*, int, int); [JScript] public static function Copy(Array, int, Array, int, int); Copies a range of elements from an Array starting at the specified source index and pastes them to another Array starting at the specified destination index. The length and the indexes are specified as 64-bit integers. [Visual Basic] Overloads Public Shared Sub Copy(Array, Long, Array, Long, Long) [C#] public static void Copy(Array, long, Array, long, long); [C++] public: static void Copy(Array*, __int64, Array*, __int64, __int64); [JScript] public static function Copy(Array, long, Array, long, long); Example The following code example shows how to copy from one Array of type Object to another Array of type integer. [Visual Basic] Imports System Imports Microsoft.VisualBasic Public Class SamplesArray Public Shared Sub Main() ' Creates and initializes a new Array of type Int32. Dim myIntArray As Array = _ Array.CreateInstance(Type.GetType("System.Int32"), 5) Dim i As Integer For i = myIntArray.GetLowerBound(0) To myIntArray.GetUpperBound(0) myIntArray.SetValue(i + 1, i) Next i ' Creates and initializes a new Array of type Object. Dim myObjArray As Array = _ Array.CreateInstance(Type.GetType("System.Object"), 5) For i = myObjArray.GetLowerBound(0) To myObjArray.GetUpperBound(0) myObjArray.SetValue(i + 26, i) Next i ' Displays the initial values of both arrays. Console.WriteLine("Int32 array:") PrintValues(myIntArray) Console.WriteLine("Object array:") PrintValues(myObjArray) ' Copies the first element from the Int32 array to the Object array. Array.Copy(myIntArray, myIntArray.GetLowerBound(0), myObjArray, _ myObjArray.GetLowerBound(0), 1) ' Copies the last two elements from the Object array to the Int32 array. Array.Copy(myObjArray, myObjArray.GetUpperBound(0) - 1, myIntArray, _ myIntArray.GetUpperBound(0) - 1, 2) ' Displays the values of the modified arrays. Console.WriteLine("Int32 array - Last two elements should now be " _ + "the same as Object array:") PrintValues(myIntArray) Console.WriteLine("Object array - First element should now be the " _ + "same as Int32 array:") PrintValues(myObjArray) End Sub Public Shared Sub PrintValues(myArr As Array) Dim myEnumerator As System.Collections.IEnumerator = _ myArr.GetEnumerator() Dim i As Integer = 0 Dim cols As Integer = myArr.GetLength((myArr.Rank - 1)) While myEnumerator.MoveNext() If i < cols Then i += 1 Else Console.WriteLine() i = 1 End If Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current) End While Console.WriteLine() End Sub End Class ' This code produces the following output. ' ' Int32 array: ' 1 2 3 4 5 ' Object array: ' 26 27 28 29 30 ' Int32 array - Last two elements should now be the same as Object array: ' 1 2 3 29 30 ' Object array - First element should now be the same as Int32 array: ' 1 27 28 29 30 [C#] using System; public class SamplesArray { public static void Main() { // Creates and initializes a new Array of type Int32. Array myIntArray=Array.CreateInstance( Type.GetType("System.Int32"), 5 ); for ( int i = myIntArray.GetLowerBound(0); i <= myIntArray.GetUpperBound(0); i++ ) myIntArray.SetValue( i+1, i ); // Creates and initializes a new Array of type Object. Array myObjArray = Array.CreateInstance( Type.GetType("System.Object"), 5 ); for ( int i = myObjArray.GetLowerBound(0); i <= myObjArray.GetUpperBound(0); i++ ) myObjArray.SetValue( i+26, i ); // Displays the initial values of both arrays. Console.WriteLine( "Int32 array:" ); PrintValues( myIntArray ); Console.WriteLine( "Object array:" ); PrintValues( myObjArray ); // Copies the first element from the Int32 array to the Object array. Array.Copy( myIntArray, myIntArray.GetLowerBound(0), myObjArray, myObjArray.GetLowerBound(0), 1 ); // Copies the last two elements from the Object array to the Int32 array. Array.Copy( myObjArray, myObjArray.GetUpperBound(0) - 1, myIntArray, myIntArray.GetUpperBound(0) - 1, 2 ); // Displays the values of the modified arrays. Console.WriteLine( "Int32 array - Last two elements should now be the same as Object array:" ); PrintValues( myIntArray ); Console.WriteLine( "Object array - First element should now be the same as Int32 array:" ); PrintValues( myObjArray ); } public static void PrintValues( Array myArr ) { System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator(); int i = 0; int cols = myArr.GetLength( myArr.Rank - 1 ); while ( myEnumerator.MoveNext() ) { if ( i < cols ) { i++; } else { Console.WriteLine(); i = 1; } Console.Write( "\t{0}", myEnumerator.Current ); } Console.WriteLine(); } } /* This code produces the following output. Int32 array: 1 2 3 4 5 Object array: 26 27 28 29 30 Int32 array - Last two elements should now be the same as Object array: 1 2 3 29 30 Object array - First element should now be the same as Int32 array: 1 27 28 29 30 */ [C++] #using <mscorlib.dll> using namespace System; void PrintValues( Array* myArr ); void main() { // Creates and initializes a new Array instance of type Int32. Array* myIntArray=Array::CreateInstance( Type::GetType("System.Int32"), 5 ); for ( int i = myIntArray->GetLowerBound(0); i <= myIntArray->GetUpperBound(0); i++ ) myIntArray->SetValue( __box(i+1), i ); // Creates and initializes a new Array instance of type Object. Array* myObjArray = Array::CreateInstance( Type::GetType("System.Object"), 5 ); for ( int i = myObjArray->GetLowerBound(0); i <= myObjArray->GetUpperBound(0); i++ ) myObjArray->SetValue( __box(i+26), i ); // Displays the initial values of both arrays. Console::WriteLine( "Int32 array:" ); PrintValues( myIntArray ); Console::WriteLine( "Object array:" ); PrintValues( myObjArray ); // Copies the first element from the Int32 array to the Object array. Array::Copy( myIntArray, myIntArray->GetLowerBound(0), myObjArray, myObjArray->GetLowerBound(0), 1 ); // Copies the last two elements from the Object array to the Int32 array. Array::Copy( myObjArray, myObjArray->GetUpperBound(0) - 1, myIntArray, myIntArray->GetUpperBound(0) - 1, 2 ); // Displays the values of the modified arrays. Console::WriteLine( "Int32 array - Last two elements should now be the same as Object array:" ); PrintValues( myIntArray ); Console::WriteLine( "Object array - First element should now be the same as Int32 array:" ); PrintValues( myObjArray ); } void PrintValues( Array* myArr ) { System::Collections::IEnumerator* myEnumerator = myArr->GetEnumerator(); int i = 0; int cols = myArr->GetLength( myArr->Rank - 1 ); while ( myEnumerator->MoveNext() ) { if ( i < cols ) { i++; } else { Console::WriteLine(); i = 1; } Console::Write( "\t{0}", myEnumerator->Current ); } Console::WriteLine(); } /* This code produces the following output. Int32 array: 1 2 3 4 5 Object array: 26 27 28 29 30 Int32 array - Last two elements should now be the same as Object array: 1 2 3 29 30 Object array - First element should now be the same as Int32 array: 1 27 28 29 30 */ [JScript] import System; // Creates and initializes a new System.Array of type Int32. var myIntArray : System.Array = System.Array.CreateInstance( Type.GetType("System.Int32"), 5 ); for ( var i : int = myIntArray.GetLowerBound(0); i <= myIntArray.GetUpperBound(0); i++ ) myIntArray.SetValue( Int32(i+1), i ); // Creates and initializes a new Array of type Object. var myObjArray : System.Array = System.Array.CreateInstance( Type.GetType("System.Object"), 5 ); for ( var j : int = myObjArray.GetLowerBound(0); j <= myObjArray.GetUpperBound(0); j++ ) myObjArray.SetValue( Int32(j+26), j); // Displays the initial values of both arrays. Console.WriteLine( "Int32 array:" ); PrintValues( myIntArray ); Console.WriteLine( "Object array:" ); PrintValues( myObjArray ); // Copies the first element from the Int32 array to the Object array. System.Array.Copy( myIntArray, myIntArray.GetLowerBound(0), myObjArray, myObjArray.GetLowerBound(0), 1 ); // Copies the last two elements from the Object array to the Int32 array. System.Array.Copy( myObjArray, myObjArray.GetUpperBound(0) - 1, myIntArray, myIntArray.GetUpperBound(0) - 1, 2 ); // Displays the values of the modified arrays. Console.WriteLine( "Int32 array - Last two elements should now be the same as Object array:" ); PrintValues( myIntArray ); Console.WriteLine( "Object array - First element should now be the same as Int32 array:" ); PrintValues( myObjArray ); function PrintValues( myArr : System.Array ) { var myEnumerator : System.Collections.IEnumerator = myArr.GetEnumerator(); var i : int = 0; var cols : int = myArr.GetLength( myArr.Rank - 1 ); while ( myEnumerator.MoveNext() ) { if ( i < cols ) { i++; } else { Console.WriteLine(); i = 1; } Console.Write( "\t{0}", myEnumerator.Current ); } Console.WriteLine(); } /* This code produces the following output. Int32 array: 1 2 3 4 5 Object array: 26 27 28 29 30 Int32 array - Last two elements should now be the same as Object array: 1 2 3 29 30 Object array - First element should now be the same as Int32 array: 1 27 28 29 30 */ See Also Array Class | Array Members | System Namespace | Managed Extensions for C++ Programming Syntax based on .NET Framework version 1.1.4322. November 15, 2002. -------------------------------------------------------------------------------- Send comments on this topic. © 2001-2002 Microsoft Corporation. All rights reserved. Jon Skeet <skeet@pobox.com> wrote in message news:<MPG.19b518258dfb6f1598a38b@news.microsoft.com>... > Fergus Cooney <wollus@tesco.net> wrote: > > We're talking that strictly? > > > > The info for C# doesn't have it, but the info for C++ does. > > The info for C# *does* say what happens when the arrays overlap. I've > got it right here in front of me, with the C# filter applied: > > <quote> > If sourceArray and destinationArray overlap, this method behaves as if > the original values of sourceArray were preserved in a temporary > location before destinationArray is overwritten. > </quote> > > That's looking on the form which takes (Array, Array, Int32) as its > parameters. It's the third paragraph in the remarks. > > There's no need to switch to the C++ filter in order to understand what > the semantics are. |
|
|
|
#10 |
|
Guest
Posts: n/a
|
Daniel Goldman <hhh12347@yahoo.com> wrote:
> As I said, the documentation for Array.Copy did not have the > answer. It repeatedly says "another array", and leaves out > "same array", which is misleading. It has a lot of text, does > not have either of the key points Fergus found in other pages. It does if you go down to *any* of the specific methods, which is *always* a good idea. The Array.Copy overload list is just an overview - it doesn't specify parameters, exceptions etc either. My point is that you certainly don't have to switch to the C++ filter in order to get the specifics. As you said before, documentation should clearly state what a function does - and the documentation does exactly that, if you look at any *specific* method rather than just the overload list. -- Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet/ If replying to the group, please do not mail me too |
|
![]() |
|
| Thread Tools | |
| Rate This Thread | |
|
|

Main Page 

