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

Replacement for memcpy call in C++

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 replacement
in C++ for this call.

Feb 6 '08 #1
8 5178
On 2008-02-07 09:40:58 -0500, Richard Herring <ju**@[127.0.0.1]said:
>
Why not simply use std::copy? (don't forget pointers are iterators too ;-)

It's a good deal safer, and likely to be just as efficient, because
it's probably specialised to call memmove or its moral equivalent
whenever it's safe to do so.
Given two pointers and a length, copy is not safer than memcpy.

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

Feb 7 '08 #2
In message <2008020710152416807-pete@versatilecodingcom>, Pete Becker
<pe**@versatilecoding.comwrites
>On 2008-02-07 09:40:58 -0500, Richard Herring <ju**@[127.0.0.1]said:
> Why not simply use std::copy? (don't forget pointers are iterators
too ;-)
It's a good deal safer, and likely to be just as efficient, because
it's probably specialised to call memmove or its moral equivalent
whenever it's safe to do so.

Given two pointers and a length, copy is not safer than memcpy.
What, even if they point to non-POD objects?

--
Richard Herring
Feb 7 '08 #3
On 2008-02-07 11:09:04 -0500, Richard Herring <ju**@[127.0.0.1]said:
In message <2008020710152416807-pete@versatilecodingcom>, Pete Becker
<pe**@versatilecoding.comwrites
>On 2008-02-07 09:40:58 -0500, Richard Herring <ju**@[127.0.0.1]said:
>> Why not simply use std::copy? (don't forget pointers are iterators too ;-)
It's a good deal safer, and likely to be just as efficient, because
it's probably specialised to call memmove or its moral equivalent
whenever it's safe to do so.

Given two pointers and a length, copy is not safer than memcpy.

What, even if they point to non-POD objects?
Sighg. Okay: given that memcpy is acceptable for the two pointers and
the length, copy is not safer than memcpy.

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

Feb 7 '08 #4
In message <2008020711353816807-pete@versatilecodingcom>, Pete Becker
<pe**@versatilecoding.comwrites
>On 2008-02-07 11:09:04 -0500, Richard Herring <ju**@[127.0.0.1]said:
>In message <2008020710152416807-pete@versatilecodingcom>, Pete Becker
<pe**@versatilecoding.comwrites
>>On 2008-02-07 09:40:58 -0500, Richard Herring <ju**@[127.0.0.1]said:

Why not simply use std::copy? (don't forget pointers are iterators
too ;-)
It's a good deal safer, and likely to be just as efficient,
because it's probably specialised to call memmove or its moral
equivalent whenever it's safe to do so.
Given two pointers and a length, copy is not safer than memcpy.
What, even if they point to non-POD objects?

Sighg. Okay: given that memcpy is acceptable for the two pointers and
the length, copy is not safer than memcpy.
Agreed, but it's no less safe and probably no slower.

(And if the ranges overlap, you may have to stop and think whether you
really meant memcpy and not memmove.)

I just don't like to see solutions implying that copying objects is
merely a matter of shuffling bits around. Sooner or later one gets
bitten.

--
Richard Herring
Feb 7 '08 #5
On Feb 7, 4:15 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2008-02-07 09:40:58 -0500, Richard Herring <ju**@[127.0.0.1]said:
Why not simply use std::copy? (don't forget pointers are
iterators too ;-)
It's a good deal safer, and likely to be just as efficient, because
it's probably specialised to call memmove or its moral equivalent
whenever it's safe to do so.
Given two pointers and a length, copy is not safer than memcpy.
There are two potential errors with memcpy: the length is wrong,
and that memcpy doesn't have appropriate semantics for the type
being copied. Copy only suffers from the first of these.

--
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
Feb 8 '08 #6
On Feb 7, 7:16 pm, Richard Herring <ju**@[127.0.0.1]wrote:
In message <2008020711353816807-pete@versatilecodingcom>, Pete Becker
<p...@versatilecoding.comwrites
On 2008-02-07 11:09:04 -0500, Richard Herring <ju**@[127.0.0.1]said:
In message <2008020710152416807-pete@versatilecodingcom>, Pete Becker
<p...@versatilecoding.comwrites
On 2008-02-07 09:40:58 -0500, Richard Herring <ju**@[127.0.0.1]said:
>> Why not simply use std::copy? (don't forget pointers are iterators
too ;-)
It's a good deal safer, and likely to be just as efficient,
because it's probably specialised to call memmove or its moral
equivalent whenever it's safe to do so.
Given two pointers and a length, copy is not safer than memcpy.
What, even if they point to non-POD objects?
Sighg. Okay: given that memcpy is acceptable for the two pointers and
the length, copy is not safer than memcpy.
Agreed, but it's no less safe and probably no slower.
(And if the ranges overlap, you may have to stop and think whether you
really meant memcpy and not memmove.)
If the ranges overlap, you may have to think whether you need to
use reverse iterators or not with std::copy. If you don't know
whether they overlap or not, memmove always works, but you can't
use std::copy. (Note that there is no standard conformant way
of determining whether two ranges overlap, given only the
pointers and their length. memmove cannot be implemented in
standard conformant, portable C.)

--
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
Feb 8 '08 #7
On 2008-02-08 04:46:10 -0500, James Kanze <ja*********@gmail.comsaid:
On Feb 7, 4:15 pm, Pete Becker <p...@versatilecoding.comwrote:
>On 2008-02-07 09:40:58 -0500, Richard Herring <ju**@[127.0.0.1]said:
>>Why not simply use std::copy? (don't forget pointers are
iterators too ;-)
>>It's a good deal safer, and likely to be just as efficient, because
it's probably specialised to call memmove or its moral equivalent
whenever it's safe to do so.
>Given two pointers and a length, copy is not safer than memcpy.

There are two potential errors with memcpy: the length is wrong,
and that memcpy doesn't have appropriate semantics for the type
being copied. Copy only suffers from the first of these.
Right. But given that memcpy works, which is the premise for replacing
it, copy is not inherently "a good deal safer."

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

Feb 8 '08 #8
In message <2008020809065616807-pete@versatilecodingcom>, Pete Becker
<pe**@versatilecoding.comwrites
>On 2008-02-08 04:46:10 -0500, James Kanze <ja*********@gmail.comsaid:
>On Feb 7, 4:15 pm, Pete Becker <p...@versatilecoding.comwrote:
>>On 2008-02-07 09:40:58 -0500, Richard Herring <ju**@[127.0.0.1]said:
>>>Why not simply use std::copy? (don't forget pointers are
iterators too ;-)
>>>It's a good deal safer, and likely to be just as efficient, because
it's probably specialised to call memmove or its moral equivalent
whenever it's safe to do so.
>>Given two pointers and a length, copy is not safer than memcpy.
There are two potential errors with memcpy: the length is wrong,
and that memcpy doesn't have appropriate semantics for the type
being copied. Copy only suffers from the first of these.

Right. But given that memcpy works, which is the premise for replacing
it, copy is not inherently "a good deal safer."
"Given that memcpy works" is *not* a premise. It's a proviso that
practically begs the question. The original premise was this:
>>>>If the source is a raw stream of bytes, (say from a communications
line), or the source and destination are disparate data types and
can not be made the same type (for whatever reason), then just memcpy.
and in the second case ("disparate data types") naively applying memcpy
is unlikely to have a happy outcome.

--
Richard Herring
Feb 8 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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;...
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...
1
by: remove the CAPS | last post by:
Hi, all. This question came up in a code review: are null pointers legal as arguments to memcpy? That is, is the program at the end of this article strictly conforming? (Of course, I check the...
3
by: ebrahimbandookwala | last post by:
HI everyone I am supposed to pass back a pointer to a struct from a function whos definition cannot be changed flight_t * get_item(field_t field , void * data) the problem I encounter is...
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)...
6
by: Eric | last post by:
Tell me why the following doesn't work... void MyClass::StoreCommand( struct command cmdToStore ) { memcpy( (char*)&theCommand, (char*)&cmdToStore, sizeof( struct command ) ); } theCommand...
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?
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.