C ++: What is the fundamental difference between memset, memcpy, and strcpy?

Source: Internet
Author: User

# Include "memory. H"

Memset is used to set all memory spaces to a specific character. It is generally used to initialize the defined string to ''or '/0'. For example, char a [100]. memset (A, '/0', sizeof ());

Memcpy is used for memory copying. You can use it to copy any data type object and specify the length of the copied data. For example, char a [100], B [50]; memcpy (B, a, sizeof (B); note that if sizeof (a) is used, the memory address of B may overflow.

Strcpy can only copy strings. It ends copying when '/0' is encountered. For example, char a [100], B [50]; strcpy (A, B ); if strcpy (B, A) is used, check whether the length of the string in A (before the first '/0') exceeds 50 bits. If it exceeds, this will cause memory address overflow of B.

Strcpy
Prototype: extern char * strcpy (char * DEST, char * SRC );
Usage: # include <string. h>
Function: Copies the string ending with null indicated by Src to the array indicated by DeST.
Note: The memory areas specified by Src and DEST cannot overlap and DEST must have sufficient space to accommodate SRC strings.
Returns the pointer to DeST.
Memcpy
Prototype: extern void * memcpy (void * DEST, void * SRC, unsigned int count );
Usage: # include <string. h>
Function: copy count bytes from the memory area indicated by Src to the memory area indicated by DeST.
Note: the memory areas specified by Src and DEST cannot overlap. The function returns a pointer to DeST.
Memset
Prototype: extern void * memset (void * buffer, int C, int count );
Usage: # include <string. h>
Function: sets the first count byte of the memory area referred to by buffer to character C.
Note: The pointer to the buffer is returned.

What is assert () used?

Assert () is a macro that is often used when debugging a program. When the program is running, it calculates the expressions in parentheses. If the expression is false (0), the program reports an error and terminates the execution. If the expression is not 0, execute the following statement. This macro often used to determine whether the program contains clearly illegal data. If the program is terminated, it will not cause serious consequences, but also facilitate searching for errors. For example, variable N should not be 0 in the program. If it is 0, an error may occur. You can write the program as follows:
......
Assert (n! = 0 );
K = 10/N;
......
Assert is valid only in the debug version. If it is compiled into the release version, it is ignored.
Assert () has similar functions. It is a function specified in the ansi c standard. An important difference between assert and assert is that it can be used in the release version.

System ("pause ");
Pause the program, press any key to continue, the screen will print, "press any key to continue ....." Eliminating the need to use getchar ();

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.