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

Questions of copying memory

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
6 4778
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
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
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
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
> 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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bob Halley | last post by:
In dnspython I have a set class, SimpleSet. (I don't use Python 2.3's sets.Set class so I can keep supporting Python 2.2, and because the objects in my sets are mutable). The SimpleSet class has...
12
by: Ross Boylan | last post by:
I am trying to understand under what circumstances destructors get called with std::vector. I have an application in which I will put real objects, not just pointers, in the vector. 1. The...
45
by: Pat | last post by:
Hi, 1. Any faster method (other than for-loop) to initialize array? 2. Does there have similar C++ function to replace C's "fprintf"? Thanks. Pat
21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
14
by: MSR | last post by:
I have a couple of questions. 1. Copy Constructor. class A { private: int a1; double d1; char *ptr;
4
by: Lafer | last post by:
Hello, I am attempting to write a terminal interface program using PowerPC Assembly/C, where I am using an integer-to-ASCII conversion algorithm. Unfortunately, I have run into some difficulties...
21
by: Method Man | last post by:
Just a few theoretical questions I had on 'memmove': 1. Is there ever a good reason to use memcpy instead of memmove? 2. Is memmove useful to copy structs (as opposed to = operator)? 3. In...
10
by: James S. Singleton | last post by:
Thanks everybody for your replies. I gather that: a) How to obtain the size of the memory region pointed to by ptr in realloc(ptr, size) is implementation-dependent. b) Implementing...
9
by: Mr John FO Evans | last post by:
Am looking at adding structures to an embedded C emulator but am wondering what the standard is for copying structures(ie structa=structb) which contain pointers to memory and which are therefore...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.