[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

newbie)Technically what's the difference between memset() and memcpy() functions?

sam

9/10/2008 12:59:00 PM

(newbie)Technically what's the difference between memset() and
memcpy() functions?
19 Answers

Bart Friederichs

9/10/2008 1:17:00 PM

0

sam wrote:
> (newbie)Technically what's the difference between memset() and
> memcpy() functions?

You could read the manual ...

memset() sets a memory area to a certain value
memcpy() copies one memory area to another area

Bart

Road.Tang

9/10/2008 3:18:00 PM

0

On Sep 10, 8:59 pm, sam <sameer...@gmail.com> wrote:
> (newbie)Technically what's the difference between memset() and
> memcpy() functions?

just as the naming of the funcation.

memset is used to *set* a range of memory to the unique value.

#define BUFSIZE 1024
char buf[BUFSIZE];
memset(buf, 0, sizeof(BUFSIZE)); /* set [buf, buf+BUFSIZE) range
memory to 0 */

whereas, memcpy is used to *copy* a range of memory into another
range of memory.

char buf1[BUFSIZE];
char buf2[BUFSIZE] = { 'a', 'b', 'c' };

memcpy(buf1, buf2, sizeof(buf2)); /* copy the value of buf2 into buf1
*/

Roadt

James Kanze

9/10/2008 4:34:00 PM

0

On Sep 10, 2:59 pm, sam <sameer...@gmail.com> wrote:
> (newbie)Technically what's the difference between memset() and
> memcpy() functions?

Well, formally, they do different things. Practically, who
cares? You'd never use either in a C++ program.

--
James Kanze (GABI Software) email:james.kanze@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

red floyd

9/10/2008 8:20:00 PM

0

On Sep 10, 9:34 am, James Kanze <james.ka...@gmail.com> wrote:
> On Sep 10, 2:59 pm, sam <sameer...@gmail.com> wrote:
>
> > (newbie)Technically what's the difference between memset() and
> > memcpy() functions?
>
> Well, formally, they do different things.  Practically, who
> cares?  You'd never use either in a C++ program.
>

Not necessarily. If you are using a library with a C API, and they
require a zeroed POD struct as a param, memset is your easiest bet.

gw7rib

9/10/2008 9:23:00 PM

0

On 10 Sep, 13:59, sam <sameer...@gmail.com> wrote:
> (newbie)Technically what's the difference between memset() and
> memcpy() functions?

A more interesting question would be - what's the difference between
memcpy() and memmove()?

The answer to this one is that memcpy can go wrong if the place it's
moving from and the place it's moving to overlap. As a simple example,
suppose you ask it to copy 20 bytes from addresses [100-119] to
addresses [105-124]. It might copy the contents of [100] to [105],
then the contents of [101] to [106], etc, but when it gets to [105],
instead of copying what was originally in [105], this has already been
overwritten and so what it reads, and copies to [110], is what was
originally in [100]. So instead of getting a copy of what was in
[100-119], you get four copies of what was in [100-105].

memmove() is guaranteed not to go wrong in this way.

Hope that helps.
Paul.

James Kanze

9/11/2008 8:02:00 AM

0

On Sep 10, 10:20 pm, red floyd <redfl...@gmail.com> wrote:
> On Sep 10, 9:34 am, James Kanze <james.ka...@gmail.com> wrote:

> > On Sep 10, 2:59 pm, sam <sameer...@gmail.com> wrote:

> > > (newbie)Technically what's the difference between memset() and
> > > memcpy() functions?

> > Well, formally, they do different things. Practically, who
> > cares? You'd never use either in a C++ program.

> Not necessarily. If you are using a library with a C API, and
> they require a zeroed POD struct as a param, memset is your
> easiest bet.

The problem is that memset doesn't necessarily zero the
structure correctly. The correct solution to the above (even in
C) is to declare a static instance of the struct, and assign it
to the struct you want to zero.

--
James Kanze (GABI Software) email:james.kanze@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

ytrembla

9/11/2008 9:50:00 AM

0

In article <a17c1516-d9b7-45e4-8713-52e65d0d185c@r66g2000hsg.googlegroups.com>,
red floyd <redfloyd@gmail.com> wrote:
>On Sep 10, 9:34 am, James Kanze <james.ka...@gmail.com> wrote:
>> On Sep 10, 2:59 pm, sam <sameer...@gmail.com> wrote:
>>
>> > (newbie)Technically what's the difference between memset() and
>> > memcpy() functions?
>>
>> Well, formally, they do different things.  Practically, who
>> cares?  You'd never use either in a C++ program.
>>
>
>Not necessarily. If you are using a library with a C API, and they
>require a zeroed POD struct as a param, memset is your easiest bet.

Huh?

Have you tried:

struct MyPodStruct; // define it somewhere
MyPodStruct s = {0};


Read about structure initialisation and remove all these pointless
memset from your code. For example:
http://www.informit.com/guides/content.aspx?g=cplusplus&...

I think you can also do

MyPodStruct s = {};

but I seem to be unable to find precise information if there is a
difference between the two and I don't have access to the standard at
the moment. I don't think so since if there are too few initialisers,
the remaining are assumed to be 0. Anyone?

Yannick




ytrembla

9/11/2008 9:57:00 AM

0

In article <6adede5b-beec-41f7-9218-cdfc623194f5@r66g2000hsg.googlegroups.com>,
<gw7rib@aol.com> wrote:
>On 10 Sep, 13:59, sam <sameer...@gmail.com> wrote:
>> (newbie)Technically what's the difference between memset() and
>> memcpy() functions?
>
>A more interesting question would be - what's the difference between
>memcpy() and memmove()?
>
>The answer to this one is that memcpy can go wrong if the place it's
>moving from and the place it's moving to overlap. As a simple example,
>suppose you ask it to copy 20 bytes from addresses [100-119] to
>addresses [105-124]. It might copy the contents of [100] to [105],
>then the contents of [101] to [106], etc, but when it gets to [105],
>instead of copying what was originally in [105], this has already been
>overwritten and so what it reads, and copies to [110], is what was
>originally in [100]. So instead of getting a copy of what was in
>[100-119], you get four copies of what was in [100-105].
>
>memmove() is guaranteed not to go wrong in this way.

Strictly speaking, memcpy did not go wrong. It did exactly what was
asked for which is copy byte by byte 20 bytes from address 100 to
address 105.

I would hesitate to recommend using memmove over memcpy in the general
case based on being resilient to this programming mistake because one
of the only justification to use memcpy is its efficiency. By virtue
of protecting against the above case, memmove has to be less efficient
than memcpy. Obviously, in 95% of cases you won't care about
efficiency so should use neither memcpy nor memmove but stick with
much safer things like copy constructors, assigment operator,
std::swap and std::copy.


Yannick


Gennaro Prota

9/11/2008 11:05:00 AM

0

Yannick Tremblay wrote:
> Read about structure initialisation and remove all these pointless
> memset from your code. For example:
> http://www.informit.com/guides/content.aspx?g=cplusplus&...

Please, don't link to articles with such low quality. I did read up to sixth
line (excluding titles) and there were more technical errors than words.

--
Gennaro Prota | name.surname yahoo.com
Breeze C++ (preview): <https://sourceforge.net/projects/b...
Do you need expertise in C++? I'm available.

ytrembla

9/11/2008 4:17:00 PM

0

In article <gaau0j$m4k$1@aioe.org>,
Gennaro Prota <gennaro/prota@yahoo.com> wrote:
>Yannick Tremblay wrote:
>> Read about structure initialisation and remove all these pointless
>> memset from your code. For example:
>> <censored URL not to raise more issues>
>
>Please, don't link to articles with such low quality. I did read up to sixth
>line (excluding titles) and there were more technical errors than words.

I'll confess not to having read the introduction paragraph properly.
He seems to confuse objects, variables. types and POD, so yeah, not
fantastic. I just googled for an article to elaborate on the
subject and I guess I should not have. The code samples below are
not that bad. really :-(


However, the main point of my comment remain valid:


One should not:
---------------------------
struct MyStruct;

MyStruct s;
memset(&s, 0, sizeof(s));
---------------------------

But should simply do:
-------------------------
struct MyStruct;

MyStruct s = {};
-------------------------


Yan