By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,483 Members | 1,614 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,483 IT Pros & Developers. It's quick & easy.

memcpy equivalent in C++

P: n/a

Hi,
I would like to know if C++ has a class equivalent to
memcpy and its associated functions available in C.

Nov 19 '08 #1
Share this Question
Share on Google+
11 Replies


P: n/a
On Nov 19, 9:11*am, mthread <rjk...@gmail.comwrote:
* * * * * *I would like to know if C++ has a class equivalentto
memcpy and its associated functions available in C.
It is std::copy() from <algorithmheader. http://www.sgi.com/tech/stl/copy..html

--
Max
Nov 19 '08 #2

P: n/a
On 2008-11-19 10:11, mthread wrote:
Hi,
I would like to know if C++ has a class equivalent to
memcpy and its associated functions available in C.
memcpy() is also available in C++, but you should only use it for raw
data, if you use it on objects you might get nasty surprises. For
objects you should use std::copy() as Max pointed out.

--
Erik Wikström
Nov 19 '08 #3

P: n/a
Maxim Yegorushkin wrote:
On Nov 19, 9:11 am, mthread <rjk...@gmail.comwrote:
> I would like to know if C++ has a class equivalent to
memcpy and its associated functions available in C.

It is std::copy() from <algorithmheader. http://www.sgi.com/tech/stl/copy.html
std::copy is not the same thing as memcpy. std::copy honors C++
objects' copy semantics, which can be a lot more complicated than what
memcpy supports. The OP probably wants std::memcpy, from the <cstring>
header.

I wonder whether popular implementations of std::copy delegate to memcpy
for types with trivial copy constructors.
Nov 19 '08 #4

P: n/a
On 2008-11-19 14:26:34 -0500, Jeff Schwab <je**@schwabcenter.comsaid:
>
I wonder whether popular implementations of std::copy delegate to
memcpy for types with trivial copy constructors.
Yes, they do.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Nov 19 '08 #5

P: n/a
On Nov 19, 8:26 pm, Jeff Schwab <j...@schwabcenter.comwrote:
Maxim Yegorushkin wrote:
On Nov 19, 9:11 am, mthread <rjk...@gmail.comwrote:
I would like to know if C++ has a class equivalent to
memcpy and its associated functions available in C.
It is std::copy() from <algorithm>
header.http://www.sgi.com/tech/stl/copy.html
std::copy is not the same thing as memcpy. std::copy honors
C++ objects' copy semantics, which can be a lot more
complicated than what memcpy supports. The OP probably wants
std::memcpy, from the <cstringheader.
I wonder whether popular implementations of std::copy delegate
to memcpy for types with trivial copy constructors.
They may or they may not. With a good compiler, generating the
code directly inline probably results in faster code, and maybe
even smaller code.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Nov 20 '08 #6

P: n/a
Erik Wikström wrote:
On 2008-11-19 10:11, mthread wrote:
>Hi,
I would like to know if C++ has a class equivalent to
memcpy and its associated functions available in C.

memcpy() is also available in C++, but you should only use it for raw
data, if you use it on objects you might get nasty surprises. For
objects you should use std::copy() as Max pointed out.
If a class has only basic types (and possible arrays of basic types)
as members and only has the default compiler-generated copy constructor
and assignment operator, is the behavior of copying objects of that
class with memcpy() still undefined?
Nov 20 '08 #7

P: n/a
James Kanze wrote:
On Nov 19, 8:26 pm, Jeff Schwab <j...@schwabcenter.comwrote:
>I wonder whether popular implementations of std::copy delegate
to memcpy for types with trivial copy constructors.

They may or they may not. With a good compiler, generating the
code directly inline probably results in faster code, and maybe
even smaller code.
That raises another interesting question: Are there any platforms that
define both their C and C++ standard libraries on top of a shared
subset, rather than just providing C++ aliases for old C? For example,
are there any implementations that just forward both memcpy and
(sometimes) std::copy to an internal __inline_memcpy?
Nov 20 '08 #8

P: n/a
Juha Nieminen wrote:
Erik Wikström wrote:
>On 2008-11-19 10:11, mthread wrote:
>>Hi,
I would like to know if C++ has a class equivalent to
memcpy and its associated functions available in C.
memcpy() is also available in C++, but you should only use it for raw
data, if you use it on objects you might get nasty surprises. For
objects you should use std::copy() as Max pointed out.

If a class has only basic types (and possible arrays of basic types)
as members and only has the default compiler-generated copy constructor
and assignment operator, is the behavior of copying objects of that
class with memcpy() still undefined?
That depends what you consider "basic types." If the types involved are
POD, the behavior is defined (as long as neither of the objects is a
sub-object of the other). See 3.9 Types [basic.types], paragraph 3.
Nov 20 '08 #9

P: n/a
Jeff Schwab wrote:
James Kanze wrote:
>On Nov 19, 8:26 pm, Jeff Schwab <j...@schwabcenter.comwrote:
>>I wonder whether popular implementations of std::copy delegate
to memcpy for types with trivial copy constructors.

They may or they may not. With a good compiler, generating the
code directly inline probably results in faster code, and maybe
even smaller code.

That raises another interesting question: Are there any platforms
that define both their C and C++ standard libraries on top of a
shared subset, rather than just providing C++ aliases for old C? For
example, are there any implementations that just forward both
memcpy and (sometimes) std::copy to an internal __inline_memcpy?
Yes. For example one produced by a big company that also does OSs and
office packages.

Not only will memcpy be inlined by the compiler, but a for loop in
user code will also be optimized into identical machine code.
Using memcpy is NOT a magical optimization trick!
Bo Persson
Nov 20 '08 #10

P: n/a
On Nov 20, 2:03 pm, Jeff Schwab <j...@schwabcenter.comwrote:
James Kanze wrote:
On Nov 19, 8:26 pm, Jeff Schwab <j...@schwabcenter.comwrote:
I wonder whether popular implementations of std::copy delegate
to memcpy for types with trivial copy constructors.
They may or they may not. With a good compiler, generating
the code directly inline probably results in faster code,
and maybe even smaller code.
That raises another interesting question: Are there any
platforms that define both their C and C++ standard libraries
on top of a shared subset, rather than just providing C++
aliases for old C?
There are probably some, but in many cases, the C library is
bundled with the system.
For example, are there any implementations that just forward
both memcpy and (sometimes) std::copy to an internal
__inline_memcpy?
I always thought that the usual implementation of memcpy in the
C header was something like:

extern void* memcpy( void* dest, void const* src, size_t n ) ;
#define memcpy( dest, src, n ) __builtin_memcpy( dest, src, n )

(But of course, this causes problems when the library is bundled
as well.) And of course, since it is a template, the compiler
can already see all of the code for std::copy, and optimize
accordingly.

Of course, this is only valid for simple functions like memcpy.
I can't imagine any implementation using a compiler builtin for
something like strftime.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34
Nov 21 '08 #11

P: n/a
On Nov 20, 2:02 pm, Juha Nieminen <nos...@thanks.invalidwrote:
Erik Wikstrm wrote:
On 2008-11-19 10:11, mthread wrote:
I would like to know if C++ has a class equivalent to
memcpy and its associated functions available in C.
memcpy() is also available in C++, but you should only use
it for raw data, if you use it on objects you might get
nasty surprises. For objects you should use std::copy() as
Max pointed out.
If a class has only basic types (and possible arrays of basic
types) as members and only has the default compiler-generated
copy constructor and assignment operator, is the behavior of
copying objects of that class with memcpy() still undefined?
If the class is a POD struct or a POD union, the behavior is
well defined. If the class contains pointers, it may not be
what you want, but it is well defined.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34

Nov 21 '08 #12

This discussion thread is closed

Replies have been disabled for this discussion.