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

memcpy vs memmove

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

Thanks

Jun 27 '07 #1
5 18759
xdevel <xd********@gmail.comwrites:
Hi, anyone can make me an example where
memmove does not cause a memory overlapping and
where memcpy do it?

Thanks
http://groups.google.de/group/comp.l...24264d804bc9ca

or

http://tinyurl.com/3759hy

Good luck!
Jun 27 '07 #2
On 27 Giu, 15:19, Richard <rgr...@gmail.comwrote:
xdevel <xdevel1...@gmail.comwrites:
Hi, anyone can make me an example where
memmove does not cause a memory overlapping and
where memcpy do it?
Thanks

http://groups.google.de/group/comp.l...rowse_frm/thre...

or

http://tinyurl.com/3759hy

Good luck!

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!

Jun 27 '07 #3
"xdevel" <xd********@gmail.comwrote in message
news:11*********************@n2g2000hse.googlegrou ps.com...
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

--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov
--
Posted via a free Usenet account from http://www.teranews.com

Jun 27 '07 #4
xdevel <xd********@gmail.comwrites:
On 27 Giu, 15:19, Richard <rgr...@gmail.comwrote:
>xdevel <xdevel1...@gmail.comwrites:
Hi, anyone can make me an example where
memmove does not cause a memory overlapping and
where memcpy do it?
Thanks

http://groups.google.de/group/comp.l...rowse_frm/thre...

or

http://tinyurl.com/3759hy

Good luck!


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '07 #5
xdevel wrote:
>
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 */

--
pete
Jul 4 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: franky.backeljauw | last post by:
Hello, following my question on "std::copy versus pointer copy versus member copy", I had some doubts on the function memcpy, as was used by tom_usenet in his reply. - Is this a c++ standard...
2
by: rinku24 | last post by:
What is the difference between memcpy and memmove? Which is more expensive?
7
by: bisforbenson | last post by:
I'm trying to compile things with gcc 4. After running the following command: % gcc -O3 -g -Wall -Wno-unused -DLINUX -D_REENTRANT -o ../../obj/egi.o -I../../include/ -I... -c egi.cpp i get...
21
by: Mac | last post by:
$ cat junk27.c #include <stdio.h> #include <string.h> int main (void) { printf("The difference between memcpy and memmove is %ld\n", (long int) memcpy - (long int) memmove); return 0; }
6
by: novice | last post by:
Please explain with an example whts the DIFFERENCE between "memcpy" and "memmove"
14
by: somenath | last post by:
Hi All, I am trying to understand the behavior of the memcpy and memmove. While doing so I wrote a program as mentioned bellow . #include<stdio.h> #include<stdlib.h> #include<string.h> ...
8
by: mthread | last post by:
Hi, I am copying data from one buffer(this data is a binary data not string) to another buffer. Earlier when I used C, I used the memcpy function call to copy the values. Is there any equivalent...
4
by: gsi | last post by:
Hi all, memmove() is guranteed to work correctly if the objects overlap, I am not sure why this can't be done without any extra condition check in code for the memmove() implementation or...
18
by: sam | last post by:
(newbie)Technically what's the difference between memset() and memcpy() functions?
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.