The memset () function _c language in C language

Source: Internet
Author: User

C language memset () function: Set the first n bytes of memory to a specific value
header file:

#include <string.h>

The memset () function is used to set the first n bytes of the specified memory to a specific value, which is a prototype:

  void * memset (void * ptr, int value, size_t num);

Parameter description:
A pointer to the memory to be manipulated by PTR.
Value is the values to set. You can either pass values of type int to value, or you can pass values of char types, int and char can be converted to each other based on ASCII code.
NUM is the first num byte of PTR, and size_t is the unsigned int.

The function Description memset () sets the value of the first num byte of the memory area that the PTR refers to, and then returns a pointer to PTR.

memset () You can set all the memory space to a specific value, so it is often used to initialize the character array. For example:

Char str[20];
memset (str, ' C ', sizeof (str)-1);

Return value returns a pointer to PTR.

Note: The parameter value is declared as int, but must be unsigned char, so the range is between 0 and 255.

Example:
Copy a plain text new window

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
  // Can not be declared as char *str = "http://c.biancheng.net";
  Char str[] = "http://c.biancheng.net";
  memset (str, '-', 7);
  Puts (str);
  System ("pause");
  return exit_success;
}

Execution results:

-------C.biancheng.net

The difference between Memmove and memcpy
memcpy and Memmove () are the library functions in C language, in the header file string.h, the role is to copy a certain length of memory content, the prototype is as follows:

void *memcpy (void *dst, const void *SRC, size_t count);

void *memmove (void *dst, const void *SRC, size_t count); 

Their role is the same, the only difference is that when the memory occurs when local overlap, memmove ensure that the results of the copy is correct, memcpy does not guarantee the correctness of the results of the copy.

In the first case, there is no problem with the overlapping areas of the copy, and the content can be copied correctly.
In the second case, the problem appears to be two bytes to the right, and the original content of the two bytes is first overwritten and not saved. So the next copy is a copy of the content that has already been overwritten, which is obviously problematic.
In fact, memcpy is just a subset of Memmove.

The C language implementation of the two is very simple, interested friends can go and see. In practice, both functions are implemented in a compilation.

Memmove can ensure that copy is correct in the copy two memory with overlapping areas, while memcopy is not, but memcopy faster than memmove, such as:
Char s[] = "1234567890";
char* P1 = s;
char* P2 = s+2;
The results of memcpy (P2, p1, 5) and Memmove (P2, p1, 5) may be different, memmove () can copy the P1 's first 5 characters "12345" correctly to P2, and the result of memcpy () is not necessarily correct

memcpy (), Memmove () and memccpy ()

The function of these three functions is to copy a chunk of memory to another block of memory. The first two functions differ in how they deal with memory area overlap (overlapping). The function of the third function is also to copy memory, but stop copying immediately if a particular value is encountered.
For a library function, you should use the Memmove () function because there is no way to know the memory area passed to him. With this function, you can guarantee that no memory block overlap problem occurs. For applications, the memcpy () function can be safely used because the code "knows" that two blocks of memory do not overlap.

About the implementation of Memmove:

void *mymemmove (void *dest, const void *SRC, size_t N)
{
  char temp[n];
  int i;
  char *d = dest;
  const char *s = src;

  for (i = 0; i < n; i++) 
    temp[i] = s[i];
  for (i = 0; i < n; i++) 
    d[i] = temp[i];

  return dest;
}

About the implementation of memcpy:

void *mymemcpy (void *dest, const void *SRC, size_t N)
{
  char *d = dest;
  const char *s = src;
  int *di;
  const int *SI;
  int r = n% 4;
  
  while (r--)
    *d++ = *s++;
  DI = (int *) D;
  Si = (const int*) s;
  n/= 4;
  while (n--)
    *di++ = *si++;

  return dest;
}

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.