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

Questions of copying memory

P: n/a
std::copy() is a function from C++ library. Is memcpy() a function from C
library, or is it re-implemented in C++ library?

Why people say "In C++, don't use memcpy for non-POD types?

What is POD or non-POD types?

Why people say std::copy function is _safer_ than memcpy?

Thanks for your comments!
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Mon, 09 Aug 2004 04:56:18 GMT, ziwu <zi*****@southbay.com> wrote:
std::copy() is a function from C++ library. Is memcpy() a function from C
library, or is it re-implemented in C++ library?
Its from the C library, which means yu can use it in C++.

Why people say "In C++, don't use memcpy for non-POD types?

What is POD or non-POD types?

Its complicated, but if you can declare it in C then it's a POD type,
otherwise its a non-POD type. That might not be exactly right but it's
close and its the intent behind the concept. POD types are compatible with
C.
Why people say std::copy function is _safer_ than memcpy?


Because std::copy calls the copy constructor for the objects you are
copying, memcpy does not. If your object cannot be copied correctly
without calling its copy constructor then memcpy is going to fail.

john
Jul 22 '05 #2

P: n/a
John Harrison wrote:

std::copy calls the copy constructor for the objects you are
copying,


John,

I think you're mistaken about that. Here's a straightforward
implementation of std::copy:

template <class InputIterator, class OutputIterator>
OutputIterator copy (InputIterator first, InputIterator last,
OutputIterator result)
{
while (first != last)
{
*result = *first;
++first;
++result;
}
return result;
}

Where is the need for copy construction of "the objects you are copying"
(as opposed to the iterators)?

--
Russell Hanneken
eu*******@cbobk.pbz
Use ROT13 to decode my email address.
Jul 22 '05 #3

P: n/a
On Mon, 09 Aug 2004 06:49:42 GMT, Russell Hanneken <me@privacy.net> wrote:
John Harrison wrote:
std::copy calls the copy constructor for the objects you are copying,


John,

I think you're mistaken about that. Here's a straightforward
implementation of std::copy:

template <class InputIterator, class OutputIterator>
OutputIterator copy (InputIterator first, InputIterator last,
OutputIterator result)
{
while (first != last)
{
*result = *first;
++first;
++result;
}
return result;
}

Where is the need for copy construction of "the objects you are copying"
(as opposed to the iterators)?


Apologies, I meant copy assignment operator not copy constructor.

john
Jul 22 '05 #4

P: n/a
On Mon, 09 Aug 2004 08:23:05 +0100, John Harrison
<jo*************@hotmail.com> wrote:
On Mon, 09 Aug 2004 06:49:42 GMT, Russell Hanneken <me@privacy.net>
wrote:
John Harrison wrote:
std::copy calls the copy constructor for the objects you are copying,


John,

I think you're mistaken about that. Here's a straightforward
implementation of std::copy:

template <class InputIterator, class OutputIterator>
OutputIterator copy (InputIterator first, InputIterator last,
OutputIterator result)
{
while (first != last)
{
*result = *first;
++first;
++result;
}
return result;
}

Where is the need for copy construction of "the objects you are
copying" (as opposed to the iterators)?


Apologies, I meant copy assignment operator not copy constructor.

john


Even that's not strictly accurate. But the point is that an assignment
operator will be called if you use std::copy, whereas memcpy will just
copy bytes.

Another difference, that is obvious from the above code, is that std::copy
can copy objects of one type to another type, whereas memcpy can only copy
objects of the same type.

john
Jul 22 '05 #5

P: n/a
> std::copy() is a function from C++ library. Is memcpy() a function from C
library, or is it re-implemented in C++ library?
memcpy is a function from the C library.
Why people say "In C++, don't use memcpy for non-POD types?

What is POD or non-POD types?
If you have to ask, you shouldn't be using memcpy at all.
Why people say std::copy function is _safer_ than memcpy?


Because there are some cases where memcpy doesn't work and std::copy does.
I can't think of any realistic cases where std::copy doesn't work and memcpy
does.
Jul 22 '05 #6

P: n/a
"Andrew Koenig" <ar*@acm.org> wrote in message news:<Iq*********************@bgtnsc04-news.ops.worldnet.att.net>...
Why people say std::copy function is _safer_ than memcpy?


Because there are some cases where memcpy doesn't work and std::copy does.
I can't think of any realistic cases where std::copy doesn't work and memcpy
does.


If by "doesn't work", you mean where the specific behaviour is incorrect, I
agree that there isn't any place where memcpy() will work and std::copy
will not.

But, if performance requirements are necessary, memcpy may be signficantly
faster then std::copy. I say may because it depends on the specific hardware
architecture (x86 family supprts an intrinsic memcpy opcode), how good your
optimizer is and the specific data being copied.

That said, do not use memcpy if you need std::copy for C++ object semantics.
Quickly getting incorrect results is not a good way to secure your job.

The best way is to wrap your copy needs inside a template that uses type traits
to decide whether to use memcpy or std::copy.

samuel
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.