Computer Programming
C Programming
The Difference Between

Difference between memcpy and memmove in C with example?

Top Answer
User Avatar
Wiki User
Answered 2016-02-12 15:56:59

Its similar to typing without insert key ON and OFF

str1="strings are good");


returns------ strings are are good


returns------ strings are read

memcpy - just copies from source to destination.

memmove - copies from source to destination if buffers overlap, every character is read before another character is written to the same location

memcpy() copies the bytes of data between memory blocks. If the block of memory overlaps, the function might not work properly. Use memmove() to deal with overlapping memory blocks.

memmove() is very much like memcpy() but very flexible as it handles overlapping of memory blocks.

User Avatar

Your Answer


Still have questions?

Related Questions

What is the difference between the functions memmove and memcpy?

memmove handles the case where the source memory and destination memory overlap.

What is difference between memcpy and memmove?

memmove ensure that the original source bytes in an overlapping region are copied before being overwritten. memcpy's behaviour is undefined if the source and the destination overlap.

Difference in Functions memmove and memcpy in c language?

hi frends, void *memmove(void *DST, const void *SRC, size_t LENGTH); This function moves LENGTH characters from the block of memory start- ing at `*SRC' to the memory starting at `*DST'. `memmove' reproduces the characters correctly at `*DST' even if the two areas overlap. void* memcpy(void *OUT, const void *IN, size_t N); This function copies N bytes from the memory region pointed to by IN to the memory region pointed to by OUT. If the regions overlap, the behavior is undefined.

How would you use the functions memcpy memset memmove?

The memset() FunctionTo set all the bytes in a block of memory to a particular value, use memset(). The function prototype is void * memset(void *dest, int c, size_t count); The argument dest points to the block of memory. c is the value to set, and count is the number of bytes, starting at dest, to be set. Note that while c is a type int, it is treated as a type char. In other words, only the low-order byte is used, and you can specify values of c only in the range 0 through 255.Use memset() to initialize a block of memory to a specified value. Because this function can use only a type char as the initialization value, it is not useful for working with blocks of data types other than type char, except when you want to initialize to 0. In other words, it wouldn't be efficient to use memset() to initialize an array of type int to the value 99, but you could initialize all array elements to the value 0. memset() will be demonstrated in program below.The memcpy() Functionmemcpy() copies bytes of data between memory blocks, sometimes called buffers. This function doesn't care about the type of data being copied--it simply makes an exact byte-for-byte copy. The function prototype is void *memcpy(void *dest, void *src, size_t count); The arguments dest and src point to the destination and source memory blocks, respectively. count specifies the number of bytes to be copied. The return value is dest. If the two blocks of memory overlap, the function might not operate properly--some of the data in src might be overwritten before being copied. Use the memmove() function, discussed next, to handle overlapping memory blocks. memcpy() will be demonstrated in program below.The memmove() Functionmemmove() is very much like memcpy(), copying a specified number of bytes from one memory block to another. It's more flexible, however, because it can handle overlapping memory blocks properly. Because memmove() can do everything memcpy() can do with the added flexibility of dealing with overlapping blocks, you rarely, if ever, should have a reason to use memcpy(). The prototype is void *memmove(void *dest, void *src, size_t count); dest and src point to the destination and source memory blocks, and count specifies the number of bytes to be copied. The return value is dest. If the blocks overlap, this function ensures that the source data in the overlapped region is copied before being overwritten. Sample program below demonstrates memset(), memcpy(), and memmove().

Difference Between strcpy and memcpy?

memcpy will copy NULL bytes also, but strcpy will stop copying if it encounters NULL Byte, since it consider NULL as a terminating character. cheers, som shekhar And another thing is if we want to copy two integer arrays or some other memory which are not strings we can use memcpy. strcpy/strncpy can be used for integer copies after typecasting but the behavior is unpridictable. Strcpy usually accepts the char* as an argument but the memcpy accepts the void * as an argument. -------Ravi

What is the memcpy library used for in computer programming?

The memcpy library is used in computer programming to copy the value of numbers from a source to the memory block destination. Memcpy is frequently used in the C++ programming language.

What are the differences between memcpy vs strcpy?

memcpy is general purpose copy. and strcpy is specific for string copying. strcpy will copy the source string to destination string and terminate it with '\0' character but memcpy takes extra argument which specifies the number of bytes to copy.memcpy will not handle copying of overlapping memory. use memove instead.

What is syntax of memcpy?

memcpy function is used to copy memory area. Syntax ------ void *memcpy(void *dest, const void *src, size_t n); *dest is a destination string. *src is a source string. n is a number of characters to be copied from source string. Example: #include #include main() { char src[]="Hello World"; char des[10]; memcpy(des,src,5); printf("des:%s\n",des); //It will contain the string "Hello". }

What is better strcpy or memcpy?

They do different things, so they are uncomparable.PS: strcpy can be implemented with strlen+memcpy:char *strcpy (char *dest, const char *src){size_t len;len= strlen (src);memcpy (dest, src, len);return dest;}

Why you need a user define data type in c?

If you want record a file with specific information, for example: Imagine that you want save a file with information about students, so, imagine what type information you will need keep in your file, you don't need think a lot to imagine something such as: ID, name, address, phone, email. So the ideal use of a defined data in C is through the structure, you can put your data in your structure type and save at file. For example: #include <stdio.h> #include <string.h> #define LEN_REC 38 struct students { char id[2], name[10], address[15], phone[10], end; }; int main() { FILE *p; char Str1[ LEN_REC + 1 ]; struct students Student[3]; int i; memcpy( (void*) Student[0].id,"01",2); memcpy( (void*) Student[0].name,"Marcos Bre",10); memcpy( (void*) Student[0].address,"Queen Street,50",15); memcpy( (void*) Student[0].phone,"6409940349",10); Student[0].end = '\0'; memcpy((void*) Student[1].id,"02",2); memcpy((void*) Student[1].name,"Peter Berl",10); memcpy((void*) Student[1].address,"Queen Street,10",15); memcpy((void*) Student[1].phone,"6409940344",10); Student[1].end = '\0'; memcpy((void*) Student[2].id,"03",2); memcpy((void*) Student[2].name,"James Carr",10); memcpy((void*) Student[2].address,"Queen Street,30",15); memcpy((void*) Student[2].phone,"6409940344",10); Student[2].end = '\0'; if((p=fopen("Students.dat","w"))==NULL){ printf("Error: Open file !\n"); exit(0); }else{ fprintf(p,"%s",(char*) &Student[0] ); fprintf(p,"%s",(char*) &Student[1] ); fprintf(p,"%s",(char*) &Student[2] ); fclose(p); } /* to list your Students */ if((p=fopen("Students.dat","r"))==NULL){ printf("Error: Open file !\n"); exit(0); }else{ /* List the Students from the file */ while( fread( (void*) Str1 ,1, LEN_REC , p )){ Str1[ LEN_REC + 1 ] ='\0'; printf("%s\n", Str1 ); } fclose(p); } return 0; }

C program for string copy without using strcpy?

Use functions strlen and memcpy.

Which function is the correct choice for moving blocks of binary data that are of arbitrary size and position in memory?


Code to implement memcpy in c?

unsigned char * memcpy(unsigned char * s1, unsigned char * s2, long size) { long ix; s1= (char *)malloc(sizeof(strlen(s2))); for(ix=0; ix < size; ix++) s1[ix] = s2[ix]; return s1; }

To copy two strings using strcpy?

Using strcpy and strcat. Or sprintf. Or strlen+memcpy. There are more than solutions.

How is a bitwise copy created in c plus plus?

You could just use memcpy(3), using sizeof() to get the object size.

Write a c program to copy the string without using strcpy?

use strcat, strncpy, stpcpy, sprintf, strlen+memcpy, etc

What is built-in function in c?

1. Compiler-dependent, tipically: memcpy, memcmp, strcpy, strlen, strcmp. 2. sizeof -- but that isn't really a function

How one array is copied to another?

int a[10], b[10]; ... a= b; /* method#1 */ for (i=0; i<10; ++i) a[i]= b[i]; /* method #2 */ memcpy (a, b, sizeof (a)); /* method #3 */

How do you concatenate two strings without using strcat?

Example1:sprintf (to, "%s%", from1, from2);Example2:size_t len1= strlen (from1);memcpy (to, from1, len1);strcpy (to+len1, from2);

What is The procedure entry point could not be located in the dll msvcr71 dll?

msvcr71.dll is a module containing standard C library functions such as printf, memcpy, and cos. It is a part of the Microsoft C Runtime Library.

What are built-in functions in c programs?

There are no built-in functions in C as such. What we call built-in functions are actually part of the C standard function library, which is just a function library like any other, but one that ships with all implementations of C. The functions we specifically regard as being built-in are those functions that do not require us to include any specific library headers. These functions are imported by default, hence they are all considered built-in. They are as follows: The string management functions (strcpy, strncpy, strcmp, strncmp, strlen, strcat, strncat, strchr, strrchr, strstr and strtok), memory management functions (malloc, calloc, realloc and free), buffer manipulation functions (memcpy, memcmp, memchr, memset and memmove), character functions (isalnum, isalpha, iscntrl, isdigit, isgraph, islower, isprint, ispunct, isspace, isupper, isxdigit, tolower and toupper) and error handling functions (perror, strerror).

How do you allocate memory dynamically for a string of unknown size in c?

With function strdup (here is an implementation, if you don't have one already)char *strdup (const char *f){size_t len;char *to;len= strlen (f);to = malloc (len+1);if (to) memcpy (to, f, len+1);return to;}

Program to implement strdup in C?

char *strdup (const char *s) { size_t len; char *p; if ( !s ) return NULL; len = strlen (s); p = malloc (len+1); if (p && len) { memcpy (p, s, len); p[len] = '\0'; } return p; }

Concatenate of two strings without using stringh function in c plus plus programming?

#include<iostream> char* concat(char* str1, char* str2) { int len1 = strlen(str1); int len2 = strlen(str2); char * str = ( char* ) malloc( len1+ len2 + 1 ); char * ptr = str + len1; memcpy( str, str1, len1 ); memcpy( ptr, str2, len2 + 1 ); ptr = NULL; return( str ); } int main() { char string1[] = "This is the first string"; char string2[] = " and this is the second string."; char* string3 = concat( string1, string2 ); std::cout<<string3<<std::endl; free( string3 ); return(0); }

How do you write C plus plus string concat without builtin functions?

#include<iostream> #include<string> int main() { // the two strings to concatenate std::string str1 = "Hello "; std::string str2 = "world!"; // allocate memory to the concatenated string with null-terminator char* str3 = new char[str1.size() + str2.size() + 1]; // initialise a moving pointer char* p = str3; // copy from the first string memcpy( p, str1.c_str(), str1.size() ); // advance the pointer p += str1.size(); // copy from the second string memcpy( p, str2.c_str(), str2.size() ); // advance the pointer p += str2.size(); // set the null-terminator *p = 0; // print concatenated string std::cout << str3 << std::endl; // tidy up delete [] str3, str3 = NULL; }