473,761 Members | 9,480 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

memcpy for copying arrays

Hello,

following my question on "std::copy versus pointer copy versus member
copy", I had some doubts on the function memcpy, as was used by tom_usenet
in his reply.

- Is this a c++ standard library function? That is, can I be sure that
every c++ standard library has this function? Or is there a c++
alternative to it?

- How does this function work?

- Can this function be used to copy arrays of e.g. ints, long doubles,
.... safely?

- What else should I know about this function?

Thanks for any reply.

Regards,

Franky B.
Jul 19 '05 #1
13 17511
fr************* **@ua.ac.be wrote:
Hello,

following my question on "std::copy versus pointer copy versus member
copy", I had some doubts on the function memcpy, as was used by
tom_usenet in his reply.

- Is this a c++ standard library function?
It is.
That is, can I be sure
that every c++ standard library has this function?
Yes.
Or is there a c++ alternative to it?
std::copy.
- How does this function work?
Implementation defined.
- Can this function be used to copy arrays of e.g. ints, long doubles,
... safely?
It can be used to copy PODs safely.
- What else should I know about this function?


It is a C library function.

--
Attila aka WW
Jul 19 '05 #2
On Tue, 16 Sep 2003 12:55:09 +0200, fr************* **@ua.ac.be wrote:
Hello,

following my question on "std::copy versus pointer copy versus member
copy", I had some doubts on the function memcpy, as was used by tom_usenet
in his reply.

- Is this a c++ standard library function? That is, can I be sure that
every c++ standard library has this function? Or is there a c++
alternative to it?
It is standard in C and C++. std::memcpy is in the <cstring> header.
It is also present as ::memcpy, in the <string.h> header. std::copy
could be considered an alternative, but memcpy is still part of C++.
- How does this function work?
Depends on the implementation. Sometimes it is a compiler intrinsic
that causes appropriate inline assembler to be inserted. Sometimes it
is just a function that copies bytes in a loop. Sometimes it is a
function implemented in assembler.
- Can this function be used to copy arrays of e.g. ints, long doubles,
... safely?
You can copy any POD types with it (including built ins).
- What else should I know about this function?


The source and destination ranges cannot overlap. If they might
overlap, use memmove instead (which is usually a bit slower).

Tom
Jul 19 '05 #3
On Tue, 16 Sep 2003, tom_usenet wrote:
It is standard in C and C++. std::memcpy is in the <cstring> header.
It is also present as ::memcpy, in the <string.h> header. std::copy
could be considered an alternative, but memcpy is still part of C++.


What do you mean by "is still part of C++"? And in what sense is
std::copy an alternative (being so much slower, given the results of our
earlier tests)?
- How does this function work?


Depends on the implementation. Sometimes it is a compiler intrinsic
that causes appropriate inline assembler to be inserted. Sometimes it
is just a function that copies bytes in a loop. Sometimes it is a
function implemented in assembler.


But it can be considered as the most optimal available function to copy
arrays?
- Can this function be used to copy arrays of e.g. ints, long doubles,
... safely?


You can copy any POD types with it (including built ins).


And how is this done? E.g. how do I copy an array of doubles?
- What else should I know about this function?


The source and destination ranges cannot overlap. If they might
overlap, use memmove instead (which is usually a bit slower).


I think std::copy uses memmove (at least in gcc/libstdc++), but it is not
a bit slower, but a lot ...

Regards,

Franky B.
Jul 19 '05 #4
On Tue, 16 Sep 2003, Attila Feher wrote:
- Is this a c++ standard library function?


It is.
That is, can I be sure that every c++ standard library has this
function?


Yes.
Or is there a c++ alternative to it?


std::copy.


But if it is a c++ function, then in which sense is std::copy an
alternative?
- What else should I know about this function?


It is a C library function.


So I should use std::copy instead? Will memcpy, being a C library
function, always be available in C++?

Regards,

Franky B.
Jul 19 '05 #5
fr************* **@ua.ac.be wrote:
What do you mean by "is still part of C++"?
It means it is not removed from it, it comes with the C part.
And in what sense is
std::copy an alternative (being so much slower, given the results of
our earlier tests)?
One implementation you have seen was slower. In other implementations it
finds out you have a POD and translates to a pure call to memcopy or faster.
But it can be considered as the most optimal available function to
copy arrays?


No. In some systems memcopy is actually slower than a hand-made loop with a
Duff's device. A good C++ standard library implementation can take it into
account and make std::copy use a better algorithm.
You can copy any POD types with it (including built ins).


And how is this done? E.g. how do I copy an array of doubles?


Look at your compilers documentation. Or use google.
The source and destination ranges cannot overlap. If they might
overlap, use memmove instead (which is usually a bit slower).


I think std::copy uses memmove (at least in gcc/libstdc++), but it is
not a bit slower, but a lot ...


It might be, but that is an implementation issue.

--
WW aka Attila
Jul 19 '05 #6
fr************* **@ua.ac.be wrote:
Or is there a c++ alternative to it?
std::copy.

But if it is a c++ function, then in which sense is std::copy an
alternative?


You have (at least) two options. In other words, two alternatives. One
is an alternative to the other.
So I should use std::copy instead? Will memcpy, being a C library
function, always be available in C++?


You can use whatever you feel is appropriate (but of course memcpy is
not appropriate for non-POD types). memcpy, being a C standard library
function, is therefore part of the C++ standard library, and provided
with all standard C++ implementations .

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #7
fr************* **@ua.ac.be wrote:
Or is there a c++ alternative to it?
std::copy.


But if it is a c++ function, then in which sense is std::copy an
alternative?


In the sense that it can be used in its place (and must be used with
non-PODs).
- What else should I know about this function?


It is a C library function.


So I should use std::copy instead?


It depends. If you are trying to copy non-PODs you must. If you copy
PODs - again - it depends. But iIMHO you should not worry about it too much
until it shows up that (you need something else (like on one of your
supported platforms memcpy is dead slow). If you use PODs and it seems you
will use PODs at that part of the code forever (like doubles) I see no
reason not to use memcpy. Unfortunately with todays C++ library
implementations (in widespread use) memcpy usually will be faster than
std::copy. it should not be this way, but it is.
Will memcpy, being a C library
function, always be available in C++?


I am no future teller, but I know about no intensions to remove it. And as
of today I have access to the C++ comitee documents - and I saw no proposal
to remove it. :-) Sorry, it is funny. I believe that you can trust that as
long as C and C++ will be around memcpy will be a part of it.

--
WW aka Attila
Jul 19 '05 #8
On Tue, 16 Sep 2003, White Wolf wrote:
- What else should I know about this function?

It is a C library function.


So I should use std::copy instead?


It depends. If you are trying to copy non-PODs you must. If you copy
PODs - again - it depends. But iIMHO you should not worry about it too much
until it shows up that (you need something else (like on one of your
supported platforms memcpy is dead slow). If you use PODs and it seems you
will use PODs at that part of the code forever (like doubles) I see no
reason not to use memcpy. Unfortunately with todays C++ library
implementations (in widespread use) memcpy usually will be faster than
std::copy. it should not be this way, but it is.


So I am not the only one to state that std::copy is slower than memcpy.
Indeed, it should not be this way ...
Will memcpy, being a C library
function, always be available in C++?


I am no future teller, but I know about no intensions to remove it. And as
of today I have access to the C++ comitee documents - and I saw no proposal
to remove it. :-) Sorry, it is funny. I believe that you can trust that as
long as C and C++ will be around memcpy will be a part of it.


Okay, thanks for the information.

Regards,

Franky B.
Jul 19 '05 #9
On Tue, 16 Sep 2003 22:03:06 +0200, fr************* **@ua.ac.be wrote:
On Tue, 16 Sep 2003, tom_usenet wrote:
It is standard in C and C++. std::memcpy is in the <cstring> header.
It is also present as ::memcpy, in the <string.h> header. std::copy
could be considered an alternative, but memcpy is still part of C++.
What do you mean by "is still part of C++"?


It was part of the C standard, and, along with the rest of the C
standard library (with minor changes), it became part of the C++
standard.

And in what sense isstd::copy an alternative (being so much slower, given the results of our
earlier tests)?


std::copy is a type-safe alternative to the non-typesafe function,
std::memcpy. In addition, std::copy works with overlapped source and
destination ranges, whilst memcpy doesn't, so, really, I suppose
std::copy is an alternative to std::memmove rather than memcpy.
>- How does this function work?


Depends on the implementation. Sometimes it is a compiler intrinsic
that causes appropriate inline assembler to be inserted. Sometimes it
is just a function that copies bytes in a loop. Sometimes it is a
function implemented in assembler.


But it can be considered as the most optimal available function to copy
arrays?


It should be, but it is a quality of implementation issue. It's up to
your compiler vendor and how much time they've spent tweaking it. You
might want to read this:

http://www.cuj.com/documents/s=7990/...r/alexandr.htm
>- Can this function be used to copy arrays of e.g. ints, long doubles,
>... safely?


You can copy any POD types with it (including built ins).


And how is this done? E.g. how do I copy an array of doubles?


double source[10] = {blah};
double dest[10];
std::memcpy(des t, source, 10 * sizeof *source);
>- What else should I know about this function?


The source and destination ranges cannot overlap. If they might
overlap, use memmove instead (which is usually a bit slower).


I think std::copy uses memmove (at least in gcc/libstdc++), but it is not
a bit slower, but a lot ...


memcpy is a bit dangerous, since it can't handle overlapping ranges,
and it isn't typesafe. memmove can handle overlapping ranges, and
hence tends to be used by std::copy for copying POD types.

My benchmarks had std::copy and std::memcpy very similar on GCC:

copy : 1.11
memcpy : 1.093

A difference of a couple of percent isn't something you should worry
about.

Tom
Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
18479
by: spoc | last post by:
I have been using memcpy to copy one class to another of the same type. There are reasons why I had to do this bug am getting some odd crashes and maybe I'm doing something dodgy copying classes like this? The classes contain no dynamicaly allocated data, just standard types and arrays. Here is an example of what I'm doing, I know in isolation it may seem odd. I am presuming this will copy all data ok but not really sure of the workings and...
6
7526
by: Samee Zahur | last post by:
Hi all, I'm a little confused - my guess is memcpy is no longer (or perhaps never was) a standard c++ function, since it has very little type check into it - and can potentially create havoc for user-defined types. Now my confusion is here - a simple instantiation of the standard copy algorithm can be quite slow compared to the older memcpy for obvious reasons - specially for a large array of built-in types like int or something; so do...
1
5199
by: Amon Tse | last post by:
Dear All, I am new to unicode developement in C++. Could anyone give me an idea whether the following (1), (2) and (3) are equivalent and correct in semantics: wchar_t src = L'ABCDE'; wchar_t tgt; wcscpy(tgt, src); //(1)
33
33759
by: Case | last post by:
#define SIZE 100 #define USE_MEMCPY int main(void) { char a; char b; int n; /* code 'filling' a */
39
19646
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1) What's the difference between these 3 statements: (i) memcpy(&b, &KoefD, n); // this works somewhere in my code
15
3826
by: Frederick Gotham | last post by:
What's the canonical way to copy an array in C++? If we're copying a POD, we can use memcpy (but there could be a more efficient alternative if we know that the blocks are suitably aligned). Regardless of whether the array consists of POD's, we could use a loop such as: #include <cassert>
18
21599
by: Mark | last post by:
Hi List, I want to write a function to copy some data out of a hardware buffer. The hardware can change the contents of this buffer without it being written to by my function. I want to use memcpy to unload the data. Do I need to specify the source data as volatile in this case? What is the correct syntax for specifying that the data pointed to by the source pointer is volatile and not the pointer itself?
3
6844
by: Nope | last post by:
LO everybody, I have a string (defined as a character array), and a structure which comprises of unsigned chars, chars or char arrays. I want to copy the 'string' into the structure, in one action, using memcpy Am I correct in assuming that I can't (or shouldn't) because of structure padding? Lets say I've got a struct of {
11
8143
by: mthread | last post by:
Hi, I would like to know if C++ has a class equivalent to memcpy and its associated functions available in C.
0
9345
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10115
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5229
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3881
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3456
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.