473,323 Members | 1,537 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,323 software developers and data experts.

memcpy equivalent in C++


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
11 8043
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
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
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
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
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
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
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
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
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
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 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 21 '08 #11
On Nov 20, 2:02 pm, Juha Nieminen <nos...@thanks.invalidwrote:
Erik Wikström 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 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 21 '08 #12

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: jonathan cano | last post by:
QUESTION: In practice, lines 36 and 37 below are usually equivalent to the default copy constructor (.e.g line 33). My questions are: (a) Does ISO 14882 guarantee that lines 36 and 37 are...
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...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
3
by: barcaroller | last post by:
Does C++ have an equivalent for C's memcpy? I have two memory blocks (created using new, not malloc) and I need to copy the contents of one to the other. The memory blocks are just that; they are...
5
by: xdevel | last post by:
Hi, anyone can make me an example where memmove does not cause a memory overlapping and where memcpy do it? Thanks
11
by: kuyper | last post by:
I've run across some rather peculiar code; here are the relevant lines that left me confused : unsigned char cr_file; unsigned char num_char; Note: this declaration actually works on our...
8
by: mthread | last post by:
Hi, I am copying data from one buffer(this data is a binary data not string) to another buffer. Earlier when I used C, I used the memcpy function call to copy the values. Is there any equivalent...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: 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: 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....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.