473,385 Members | 2,069 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Buffer.BlockCopy vs. Array.Copy

2
Does anybody know the difference between the two static methods Buffer.BlockCopy and Array.Copy? It is said that Buffer.BlockCopy is faster than Array.Copy since it only checks boundary and then moves the content with the system call m_memmove.
According to the documentation though, Buffer.BlockCopy works with primitive types such as byte, char, etc.

But I've found a case where Buffer.BlockCopy doesn't work as it should (or maybe I'm missing something about Buffer.BlockCopy):

Expand|Select|Wrap|Line Numbers
  1. char[] c1 = { 'a', 'b', 'c', 'd', 'e' };
  2. char[] c2 = new char[5];
  3. Buffer.BlockCopy(c1, 0, c2, 0, 5);
  4.  
If one analyzes the content of c2 after the Buffer.BlockCopy execution, one can see that the content of isn't "abcde" but "abc"
If I do an Array.Copy instead of Buffer.BlockCopy, the content of c2 becomes "abcde" as expected.

I'm a bit confused since I always thought that those methods actually will do the same thing as long as primitive types are used.
Aug 15 '07 #1
1 8333
Buffer.BlockCopy works as bytes. The chars 2 bytes a piece. Change the size to 10.
Sep 22 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
0
by: David Smith | last post by:
I'm trying to copy data from an array that's created and filled by an external DLL into a local managed array. The basics are something like: ushort localArray = new ushort; IntPtr...
5
by: devprog | last post by:
How do I do something like below? Thanks in adv. Dim Buffer1() As Byte Dim Buffer2() As Byte Dim TotalBuffer() As Byte TotalBuffer() = Buffer1() & Buffer2()
9
by: Burkhard | last post by:
I declared a struct with fixed size buffers to send it to an unmanaged function unsafe struct RCSTKAS { public fixed Byte xValue1 ; public fixed Byte xValue2 ; ... }
7
by: toton | last post by:
Hi, I want a circular buffer or queue like container (queue with array implementation). Moreover I want random access over the elements. And addition at tail and remove from head need to be low...
3
by: vb newbie | last post by:
I need to copy the contents of one byte array to another one. I was hoping to do it quickly uisng the CopyMemory api call. However, I'm getting: FatalExecutionEngineError was detected...
4
by: nass | last post by:
hello everyone, i have a bit of problem reading char * strings from a buffer (a shared memory, pointed to by 'file_memory'). basically i have a structure in memory 'ShMem' that can be accessed by...
5
by: shofu_au | last post by:
Dear readers, Before poorly reinventing the wheel I thought I would ask if a class already exists. I need to use a circular byte buffer that is thread safe. I cannot use MSMQ, my regular...
2
by: =?Utf-8?B?U2hhdW5P?= | last post by:
I have a buffer (a byte array) There will one thread writing data to the buffer and another emptying the buffer. To 'empty' the buffer it does a Buffer.BlockCopy to move the unread data to the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.