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 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>.
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
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
> 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.
> 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... 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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);
..
..
..
|
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...
|
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;...
|
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...
|
by: Case |
last post by:
#define SIZE 100
#define USE_MEMCPY
int main(void)
{
char a;
char b;
int n;
/* code 'filling' a */
|
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...
|
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...
|
by: sam |
last post by:
(newbie)Technically what's the difference between memset() and
memcpy() functions?
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
| |