1, memcpy
Header files: #include <string.h>
Function prototypes: void *memcpy (void *dest, const void *SRC, size_t N)
function: Copy n bytes of the memory space pointed to by the pointer src to the memory space pointed to by the dest pointer
Parameter: SRC is the starting address of the original content memory, Dest is the starting address to copy to the destination address
Return value: The starting address of the target dest memory
Note: 1, memory space can not overlap;
2, memcpy for the content of the need to replicate without restrictions, so the use of a wider;
3, it is clear that the memcpy is n bytes, although the memcpy copy of the content is completely without any restrictions, such as arrays, structures and other special structures, if you want to copy the contents of the entire structure variables to the Dest memory area, It is best to use sizeof to find the full size of the content to replicate to N to maintain the integrity of the copy;
C Code:
void *memcpy (void *dest, const void *SRC, size_t N)
{
if (NULL = = Dest | | NULL = src | | N < 0) return
NULL;
Char *tempdest = (char *) dest;
Char *tempsrc = (char *) src;
while (n--> 0)
*tempdest++ = *tempsrc++;
return dest;
}
2, Memset
Header files: #include <string.h>
Function prototypes: void *memset (void *s, int c, size_t N)
Function: N bytes of memory region with S as the starting position is populated with integer c
Parameter: s is the starting position of the memory area, C is the character to be filled, n is how many bytes to populate
Return value: The starting address of the target S memory
Note: 1, n represents the number of bytes, the function is in bytes in the form of each assignment to the destination address;
2. The Memset function is also assigned in bytes, so it is not generally feasible to assign a value of not 0 to each bit in the plastic array, which is not normally possible; (there will be a program to test this description below).
C Code:
void *memset (void *s, int c, size_t N)
{
if (null = s | | | < 0) return
null;
char * tmps = (char *) s;
while (n--> 0)
*tmps++ = C;
return s;
}
The following is a test program for considerations:
#include <stdio.h>
#include <string.h>
void *memset (void *s, int c, size_t N)
{
if (NULL = s || N < 0) return
NULL;
char * tmps = (char *) s;
while (n--> 0)
*tmps++ = C;
return s;
}
int main ()
{
int buf[10];
int i;
printf ("%d\n", sizeof buf);
for (i = 0; i < i++)
printf ("buf[%d] =%d\n", I, Buf[i]);
printf ("------------------------------------\ n");
memset (buf, 1, sizeof (BUF));
for (i = 0; i < i++)
printf ("buf[%d] =%d\n", I, Buf[i]);
return 0;
}
Results:
Results analysis: You can see that the results do not get the desired 1, and do not know what is the thing;
The binary representation of 1 is: 0000 0000 0000 0000 0000 0000 0000 0001
The Memset function is also assigned in bytes, and the pointer moves only one byte at a time, assigning the value to each address until n = 0;
The result of the last assignment in four bytes is: 0000 0001 0000 0001 0000 0001 0000 0001
The decimal display is: 16843009
When the array is changed to Char buf[10], the result is as follows: (more proved to be assigned in bytes)