472,354 Members | 1,224 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 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 7404
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?
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...

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.