4

I am trying to understand the function memcpy() which is defined in the C library <string.h>

Syntax: void *memcpy(void*dst,const void*src,size_t n);

I know this function is used to copy the contents of the memory pointed by pointer src to the location pointed by the dst pointer and return the address pointed by dst.

I am not able to understand the following important statement regarding memcpy():

  • When using memcpy(), the memory address should not overlap, if it overlaps then the memcpy() is undefined.

Another query is: Is the value passed to the third argument of the function i.e size_t n always an integer value?

kmbenjel
  • 9
  • 5
  • 1
    When the standard says that some behavior is undefined, it means this https://en.wikipedia.org/wiki/Undefined_behavior – StoryTeller - Unslander Monica Mar 29 '17 at 08:20
  • Th man is telling you that the, if `src+n` address overlap `dst` the final data in `dst` can be whatever. – LPs Mar 29 '17 at 08:22
  • For `size_t` take a look at [this SO post](http://stackoverflow.com/questions/2550774/what-is-size-t-in-c) – LPs Mar 29 '17 at 08:23
  • Is it the word "overlap" you don't understand? – Jabberwocky Mar 29 '17 at 08:27
  • @Michael Walz Yes! –  Mar 29 '17 at 08:29
  • 1
    It's nothing about memory leak. If you use `memcpy` at overlapped addresses, you will only lose some data. – Holsety Mar 29 '17 at 08:32
  • I took the liberty to fix the title into something more accurate. But also, you said that `memcpy` is found in stdlib.h which is incorrect. So I'm curious what source you are learning C from, as it seems to be a bad one. The proper format of memcpy is `#include void *memcpy(void * restrict s1, const void* restrict s2, size_t n);`. Copy/paste from the C standard C11 7.24.2.1. – Lundin Mar 29 '17 at 09:43

3 Answers3

14

From the comments your problem is that you don't understand what "overlapping" means:

Overlapping means this:

Here the two memory regions src and dst do overlap:

enter image description here

But here they don't:

enter image description here

So if you have overlapping memory regions, then you cannot use memcpy but you have to use memmove.


Second question:

Yes, size_t is an unsigned integer type. The third argument is the number of bytes to copy, so it can hardly be anything else than an unsigned integer type.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • I agreed a graph will be the best answer. – Holsety Mar 29 '17 at 08:49
  • 2
    Nice graphics. Suggest adding comment about `memmove()` as an alternative. Also that `size_t` is some _unsigned_ type. With "`size_t n` is always an integer value?", some read that as "`size_t n` is always an `int`?" which it isn't. – chux - Reinstate Monica Mar 29 '17 at 14:31
3

memcpy doesn't use any temporary memory to copy from src to dst.

Let say:

  • src starts @104
  • dst starts @108
  • src = "abcdefgh"

Then 'a' will be @104 and 'e' will be @108.

Assuming char as 1 byte then after copying:

  • dst = "abcdabcd".

As n denotes length to be copied, it should always be an integer.

To copy overlapping areas, you can use memmove function which uses temporary memory to copy.

mortalis
  • 2,060
  • 24
  • 34
urvi_189
  • 31
  • 2
  • 4
    Why would a temporary memory area be required for memmove()? One pointer compare will allow the copying to start at the appropriate end so that the source is not overwritten before it is read. – ThingyWotsit Mar 29 '17 at 11:49
  • 4
    C does not specify that `memcpy()` nor `memmove()` use temporary memory or not. They may, they might not. – chux - Reinstate Monica Mar 29 '17 at 14:33
  • Linux Programmer’s Manual : The memmove() function copies n bytes from memory area src to memory area dest. The memory areas may overlap: copying takes place as though the bytes in src are first copied into a temporary array that does not overlap src or dest, and the bytes are then copied from the temporary array to dest. @ThingyWotsit – urvi_189 Mar 30 '17 at 09:18
  • @urvi_189 Emphasis on the "as though". But it's not typically implemented that way in practice. – Steve Summit Nov 17 '22 at 21:56
-1

*) The main difference between memcpy and memmove is,memcpy works on the same string but memmove works in separate memory by taking a copy of the string.
*) Due to this,overlapping happens in memcpy.
Let me explain you with an example.
I took a character array :

char s[20]="alightechs";

if i do the following operations separately,

memmove(s+5,s,7);  
memcpy(s+5,s,7); 



o/p for memmove is alighalighte  
o/p for memmove is alighalighal  

because moving or copying both happens single byte by byte.
till alighaligh, there is no problem in both the cases because dest=alighte(7 is the number of chars to copy/move),
while memcpy i'm copying from 5th position,

alightechs   
alighaechs  
alighalchs  
alighalihs  
alighaligs  
alighaligh  

till here there is no problem,it copied aligh(5 letters)
as,memcpy works on the same string,6th letter will be a and 7th letter will be l
i.,e every time the string is getting updated which results in undefined o/p as below
at 6th letter copying,
the updated string is s[20]=alighaligh
so we get now

alighaligha  
alighalighal  
krish sai
  • 1
  • 1