memcpy vs memmove

X

xdevel

Hi, anyone can make me an example where
memmove does not cause a memory overlapping and
where memcpy do it?

Thanks
 
X

xdevel



Reading that forum I see this example:

// here overlap
memmove (example +5 , example, 6);

// Her its ok:
//memcpy (example, example +5, 4);

but in both cases there is an overlap! in fact
my compiler run both and the output is coherent. Also if I change
memmove (example +5 , example, 6) with memcpy (example +5 , example,
6)!

So I don't' understand what mean in this context the word OVERLAP!
 
S

Stephen Sprunk

xdevel said:
Reading that forum I see this example:

// here overlap
memmove (example +5 , example, 6);

// Her its ok:
//memcpy (example, example +5, 4);

but in both cases there is an overlap!

No, the second example does not have any overlap; that's why it's safe to
use memcpy().
in fact my compiler run both and the output is coherent. Also if I
change memmove (example +5 , example, 6) with memcpy
(example +5 , example, 6)!

memcpy() is not _required_ to handle cases of overlap correctly; that
doesn't mean it won't, or that its behavior will even be consistent. It's
simply undefined.
So I don't' understand what mean in this context the word OVERLAP!

Overlap means that the two objects specified have one or more bytes in
common.

S
 
K

Keith Thompson

xdevel said:
Reading that forum I see this example:

// here overlap
memmove (example +5 , example, 6);

// Her its ok:
//memcpy (example, example +5, 4);

but in both cases there is an overlap! in fact
my compiler run both and the output is coherent. Also if I change
memmove (example +5 , example, 6) with memcpy (example +5 , example,
6)!

So I don't' understand what mean in this context the word OVERLAP!

No, there's no overlap in the second case. You're copying 4 bytes
starting at example+5 to example, equivalent to the following (if
example is a char*):

example[0] = example[5];
example[1] = example[6];
example[2] = example[7];
example[3] = example[8];

On the other hand, your suggested call memcpy(example+5, example, 6)
does have an overlap; the byte at example[5] is both read and written.
The fact that this happened to work when you tried it means nothing.
It's undefined behavior, which means that anything can happen; that
includes having it appear to work as you expect.
 
P

pete

xdevel said:
Hi, anyone can make me an example where
memmove does not cause a memory overlapping and
where memcpy do it?

If you have
char array[] = "123";
then
memmove(array + 1, array, 2);
will give you a string like "112".

This function call:
memcpy(array + 1, array, 2);
is undefined. It could do anything, including bad things
that you might regret, but the two most likely outcomes
would be either identical behavior to memmove,
or a resulting string like "111".

/* BEGIN new.c */

#include <stdio.h>
#include <string.h>

#define STRING "123"

void *mem_cpy(void *s1, const void *s2, size_t n);

int main(void)
{
char array[sizeof STRING];

strcpy(array, STRING);
puts(array);
memmove(array + 1, array, 2);
puts(array);
strcpy(array, STRING);
mem_cpy(array + 1, array, 2);
puts(array);
return 0;
}

void *mem_cpy(void *s1, const void *s2, size_t n)
{
unsigned char *p1 = s1;
const unsigned char *p2 = s2;

while (n-- != 0) {
*p1++ = *p2++;
}
return s1;
}

/* END new.c */
 

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
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top