Questions of copying memory

Z

ziwu

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!
 
J

John Harrison

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
 
R

Russell Hanneken

John said:
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)?
 
J

John Harrison

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
 
J

John Harrison

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
 
A

Andrew Koenig

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.
 
R

Ron Samuel Klatchko

Andrew Koenig said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,174
Messages
2,570,940
Members
47,485
Latest member
Andrewayne909

Latest Threads

Top