Connecting Tech Pros Worldwide Forums | Help | Site Map

memcpy equivalent in C++

mthread
Guest
 
Posts: n/a
#1: Nov 19 '08

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


Maxim Yegorushkin
Guest
 
Posts: n/a
#2: Nov 19 '08

re: memcpy equivalent in C++


On Nov 19, 9:11*am, mthread <rjk...@gmail.comwrote:
Quote:
* * * * * *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
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
 
Posts: n/a
#3: Nov 19 '08

re: memcpy equivalent in C++


On 2008-11-19 10:11, mthread wrote:
Quote:
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
Jeff Schwab
Guest
 
Posts: n/a
#4: Nov 19 '08

re: memcpy equivalent in C++


Maxim Yegorushkin wrote:
Quote:
On Nov 19, 9:11 am, mthread <rjk...@gmail.comwrote:
>
Quote:
> 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.
Pete Becker
Guest
 
Posts: n/a
#5: Nov 19 '08

re: memcpy equivalent in C++


On 2008-11-19 14:26:34 -0500, Jeff Schwab <jeff@schwabcenter.comsaid:
Quote:
>
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)

James Kanze
Guest
 
Posts: n/a
#6: Nov 20 '08

re: memcpy equivalent in C++


On Nov 19, 8:26 pm, Jeff Schwab <j...@schwabcenter.comwrote:
Quote:
Maxim Yegorushkin wrote:
Quote:
On Nov 19, 9:11 am, mthread <rjk...@gmail.comwrote:
Quote:
Quote:
Quote:
I would like to know if C++ has a class equivalent to
memcpy and its associated functions available in C.
Quote:
Quote:
It is std::copy() from <algorithm>
header.http://www.sgi.com/tech/stl/copy.html
Quote:
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.
Quote:
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:james.kanze@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

Juha Nieminen
Guest
 
Posts: n/a
#7: Nov 20 '08

re: memcpy equivalent in C++


Erik Wikström wrote:
Quote:
On 2008-11-19 10:11, mthread wrote:
Quote:
>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?
Jeff Schwab
Guest
 
Posts: n/a
#8: Nov 20 '08

re: memcpy equivalent in C++


James Kanze wrote:
Quote:
On Nov 19, 8:26 pm, Jeff Schwab <j...@schwabcenter.comwrote:
Quote:
Quote:
>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?
Jeff Schwab
Guest
 
Posts: n/a
#9: Nov 20 '08

re: memcpy equivalent in C++


Juha Nieminen wrote:
Quote:
Erik Wikström wrote:
Quote:
>On 2008-11-19 10:11, mthread wrote:
Quote:
>>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.
Bo Persson
Guest
 
Posts: n/a
#10: Nov 20 '08

re: memcpy equivalent in C++


Jeff Schwab wrote:
Quote:
James Kanze wrote:
Quote:
>On Nov 19, 8:26 pm, Jeff Schwab <j...@schwabcenter.comwrote:
>
Quote:
Quote:
>>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


James Kanze
Guest
 
Posts: n/a
#11: Nov 21 '08

re: memcpy equivalent in C++


On Nov 20, 2:03 pm, Jeff Schwab <j...@schwabcenter.comwrote:
Quote:
James Kanze wrote:
Quote:
On Nov 19, 8:26 pm, Jeff Schwab <j...@schwabcenter.comwrote:
Quote:
I wonder whether popular implementations of std::copy delegate
to memcpy for types with trivial copy constructors.
Quote:
Quote:
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.
Quote:
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.
Quote:
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:james.kanze@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
James Kanze
Guest
 
Posts: n/a
#12: Nov 21 '08

re: memcpy equivalent in C++


On Nov 20, 2:02 pm, Juha Nieminen <nos...@thanks.invalidwrote:
Quote:
Erik Wikström wrote:
Quote:
On 2008-11-19 10:11, mthread wrote:
Quote:
Quote:
Quote:
I would like to know if C++ has a class equivalent to
memcpy and its associated functions available in C.
Quote:
Quote:
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.
Quote:
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:james.kanze@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

Closed Thread