Reprint ------------ memcpy () function usage of C function, memcpy function usage
Reprinted on http://blog.csdn.net/tigerjibo/article/details/6841531
Function prototype
Void * memcpy (void * dest, const void * src, size_t n );
Function
Data of n consecutive bytes directed by src to the starting address is copied to the space with the starting address indicated b
The following is the result of man memcpy:
# Include
Void * memcpy (void * DEST, const void * SRC, size_t N );
DescriptionThe memcpy () function copies n Bytes from memory area SRC to memoryArea DeST. The memory areas must not overlap. Use memmove (3) ifMemory areas do overlap.
Memcpy and memmoveMemmove is used to
Writing memcpy memory copy Function c ++ code example and running result (Code tutorial), memcpy writing
Memcpy () is a memory copy function. Compared with strcpy, memcpy can only copy strings. memcpy can copy any type of data.
The following code uses c ++ to write a func
When copying a string, I usually use the strcpy or strncpy function, of course, the memcpy function can also be implemented. So what's the difference between strcpy and memcpy?
Here is a piece of information I found (original address: http://www.cnblogs.com/stoneJin/archive/2011/09/16/2179248.html)
The difference between strcpy and memcpy
strcpy and
The difference between these functions is that the implementation functions and the operation objects are different.The strcpy function operates on a string to copy the source string to the target string.Objects operated by the snprintf function are not limited to strings: although the target object is a string, the source object can be a string or any basic type of data. This function is mainly used to convert (string or basic data type) to a string. If the source object is a string and the % s
Writing code is sometimes the most uncomfortable thing if faith breaks down, just like believing in religion. In my early years, I read an article about Optimization of memcpy by VC and Efficiency geek 2: copying data in C/C ++, optimisation. therefore, I firmly believe that it is difficult to write memcpy faster than the C Runtime Library. But there are two things recently that have led me to doubt this be
Memory copy memcpy () and Vmsplice () Performance comparison overviewIn the previous article, "Inter-process Big Data copy Method survey" introduced and compared three kinds of a process to read files and then copied to the B process, the test results show that when it comes to the data transfer between the memory and the disk, the splice method is best represented by avoiding multiple copies of the data between the kernel buffer and the user buffer.
What are the differences between the strcpy and memcpy functions in C language? Below with me to introduce a simple, I hope you give more comments, welcome comments to correct errors.6.2 Strings and ArraysStrings are typically stored in a character array, such as the following STR definition:
Char str[] = "123456";
Here str is an array of characters, it holds a string "123456", because the string also has a Terminator "" ",
Tag: String
Strcpy and memcpy are both standard C library functions, which have the following features:
1. strcpy provides string replication. That is, strcpy is only used for string copying. It not only copies the content of the string, but also copies the end character of the string.
The strcpy function is prototype: char * strcpy (char * DEST, const char * SRC );
Notes:
(1) the data type of the operation is char *, and the returned char *
(2) This
Problem:Functions memcpy (dest, SRC, sizeof (dest)), strncpy (dest, SRC, sizeof (dest)) and snprintf (dest, sizeof (dest), "%s", SRC) Can copy the contents of the SRC string into the dest string.Which way is the most efficient?So, which way is the best performance?Solution:1. Establishment of three documents TEST_MEMCPY.C,TEST_STRNCPY.C and TEST_SNPRINTF.C:File test_memcpy.c:
Copy Code code as follows:
david@u1110-hp:~/wrk/tmp/cstring$
Differences:From the description perspective, the two functions are basically the same. The only difference is that when DEST and SRC overlap, selecting different functions may lead to different results. Write a small program to test it:0 # I nclude 1 # I nclude 23 int main ()4 {5 Int I = 0;6 int A [10];78 For (I; I 9 {10 A [I] = I;11}1213 memcpy ( A [4], A, sizeof (INT) * 6 );1415 For (I = 0; I 16 {17 printf ("% d", a [I]);18}2021 printf ("/N ");22 r
An xmemcpy tool is used for memory copying. It is said that the memcpy provided by glibc is 10 times faster than the memcpy provided by glibc when the data is copied within 120 bytes, And there is experimental data.
This is quite surprising. Memcpy has always been very efficient. Compared with the byte copy of functions such as strcpy,
also feel that memset is used to initialize an integer array to 0, which is entirely made by people who know more about memory or are opportunistic. We must be careful when using it and understand its principles. Otherwise, many mistakes will be made.
To sum up, memset actually operates on a single byte. Similar to strset. Only strset can operate on strings. Memset can be of any type. It cannot be used in disorder.
Ii. Void * memcpy (void * DEST, co
This article transferred from: http://my.oschina.net/renhc/blog/36345If you ask for the memcpy in the interview, be careful, there are traps.First look at the explanation of the standard memcpy ():?
12
void*memcpy(void *dst, const void *src, size_tn);//If copying takes place between objects that overlap, the behavior is undefined.
The name of the sick
When we write a program, we generally pay attention to the naming of variables, we can let other people basically know the meaning of the variable. memcpy memory copy, no problem; memmove, memory move? Wrong, if you understand this, then you have to take a good look at this article, memmove or memory copy. So since memcpy and Memmove are both memory copies, what's the difference?
Firs
%rcx, %rsi movq %rax, %rdi call memcpy leaq -16(%rbp), %rax movl (%rax), %eax movl %eax, -4(%rbp) movl $0, %eax leave
The Code shows that the memory replication method occupies 7-12 rows, 6 rows in total, and the forced conversion occupies 13-15 rows, 3 rows in total, and half of the commands.
Further research is actually not limited, because the 12th rows are actually a function
Difference between strcpy and memcpy, and between strcpymemcpy
Strcpy and memcpy are both standard C library functions, which have the following features.Strcpy provides string replication. That is, strcpy is only used for string copying. It not only copies the string content, but also copies the string Terminator. strcpy_s is safer!
It is known that the prototype of the strcpy function is char * strcpy (ch
1. Implementation of the memcpy () function Void * memcpy (void * DEST, const void * SRC, size_t N ); Copy n Bytes from the starting position of the memory address in the source SRC to the starting position of the memory address in the target DeST. Void * memcpy (void * DEST, const void * Source, size_t count) {char * ret = (char *) DEST; char * dest_t = ret
In general, we often copy copies of some data when we build member functions in a class template, and generally we do not advocate copying with memcpy because the types passed in in the class template can be built-in types or non-built types. Unless you type-extract before using memcpy in a member function, the consequences of its side effects can be terrifying. memcpy
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.