473,320 Members | 2,189 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,320 software developers and data experts.

C++ version of memcpy?

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 the majority of the compilers optimise this into a
single memcpy when used with POD types? How about the popular ones like
g++ or VC6?

Or, if memcpy is still in the c++ standard, why is it so and in
which standard header file can we find it?

Samee

Jul 23 '05 #1
6 7492
Samee Zahur wrote:
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
Did you measure that or is that speculation?
- specially for a large array of built-in types like int or
something; so do the majority of the compilers optimise this into a
single memcpy when used with POD types? How about the popular ones like
g++ or VC6?
You could look into the source code or measure it.
Or, if memcpy is still in the c++ standard, why is it so and in
which standard header file can we find it?


It is a standard C++ function, simply because the whole C standard library
was made part of the C++ standard library. It is declared in <cstring> (in
namespace std) and in <string.h>.

Jul 23 '05 #2
ben
memcpy is presented in <memory> under namespace std.

The reason why both memcpy and user-defined object copying mechanisms (copy
constructor, assignment operator, explicit function) exist in the standard,
is that they perform are two distinct tasks--to copy raw memory and to copy
objects. Although the latter usually makes use of the former, it is not
always the case. If you find code uses memcpy to deal with objects, throw it
away! Likewise if you find code dealing with raw memory copying using
assignment in a for-loop, chances are high it is a suboptimal one.

"Samee Zahur" <sa*********@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
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 the majority of the compilers optimise this into a
single memcpy when used with POD types? How about the popular ones like
g++ or VC6?

Or, if memcpy is still in the c++ standard, why is it so and in
which standard header file can we find it?

Samee

Jul 23 '05 #3
ben
Sorry! My mistake! memcpy is indeed in <cstring>

ben

"Ioannis Vranos" <iv*@remove.this.grad.com> wrote in message
news:1114349120.589834@athnrd02...
ben wrote:
memcpy is presented in <memory> under namespace std.

Actually <memory> defines allocators. memcpy() is defined in header

<cstring> in the namespace std, and <string.h> in the global namespace.

--
Ioannis Vranos

http://www23.brinkster.com/noicys

Jul 23 '05 #4
> Likewise if you find code dealing with raw memory copying using
assignment in a for-loop, chances are high it is a suboptimal one


You may be wrong with this one. The following functions f and g:

void f(int *a, int *b, int n)
{
for (int i = 0; i < n; ++i)
a[i] = b[i];
}

void g(int *a, int *b, int n)
{
memcpy(a, b, sizeof(int) * n);
}

generate exactly the same machine code, under VC6, VC7, VC7.1 and also
probably under VC8. I don't know about gcc, but I suppose it also does that.

cheers,
M.
Jul 23 '05 #5
> 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

what sort of type check do you want for a function to be able to do to
copy memory? I think it's clear and completely right that a function
that copies memory from one place to another takes two addresses and
size of the memory area. There's nothing else needed. The problem is
that if you copy your own object using this function you can mess up if
you don't know what you're doing and it's not memcpy's fault in that
case - you don't copy your objects you only copy memory allocated for
them.
generate exactly the same machine code, under VC6, VC7, VC7.1 and also probably under VC8. I don't know about gcc, but I suppose it also

does that.

It's a matter of a particular implementation. Other compilers/libraries
may provide a different implementation for memcpy and in case if you
copy around megs of data in your application it would be much more
efficient to use mmx/sse/sse2 for this task. Simply ms compiler (g++
and others) provide a simple implementation where it just copies memory
byte by byte since memcpy is mostly used with short c strings I think...

Jul 23 '05 #6
generate exactly the same machine code, under VC6, VC7, VC7.1 and also probably under VC8. I don't know about gcc, but I suppose it also does that.
cheers,
M.


Seriously??? I didn't check the assembler listings output, but I never
expected these optimizations to be THIS common ... I suppose, compilers
do this for just about every POD datatypes and not just built-in ones?

Samee

Jul 23 '05 #7

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

Similar topics

13
by: franky.backeljauw | last post by:
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...
16
by: Delali Dzirasa | last post by:
I would have a number packed with its hex representation of the integer below is some sample code of what is being done. int value = 20; //in hex it is 0x14 AddData (value); .. .. ..
5
by: manya | last post by:
Ok, it's been a while since I've done the whole memcpy stuff with C++ and I'm having a hard time remembering everything. I hope, however, that you can help me with my problem. I memcpy a...
35
by: Christopher Benson-Manica | last post by:
(if this is a FAQ or in K&R2, I didn't find it) What parameters (if any) may be 0 or NULL? IOW, which of the following statements are guaranteed to produce well-defined behavior? char src;...
16
by: Amarendra GODBOLE | last post by:
Hi, I am a bit confused over the correct usage of memcpy(). Kindly help me clear the confusion. The linux manpage for memcpy(3) gives me the following prototype of memcpy(3): #include...
33
by: Case | last post by:
#define SIZE 100 #define USE_MEMCPY int main(void) { char a; char b; int n; /* code 'filling' a */
6
by: myhotline | last post by:
hi all im very confused about using memcpy and i have three questions....memcpy takes a pointer to src and a pointer to dest and copies src to destination...but im very confuzed about when to...
18
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...
18
by: sam | last post by:
(newbie)Technically what's the difference between memset() and memcpy() functions?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.