memcpy和memset

memcpy

Copies characters between buffers.

void *memcpy( void *dest, const void *src, size_tcount );

RoutineRequired HeaderCompatibility
memcpy<memory.h> or <string.h>ANSI, Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIBSingle thread static library, retail version
LIBCMT.LIBMultithread static library, retail version
MSVCRT.LIBImport library for MSVCRT.DLL, retail version

Return Value

memcpy returns the value of dest.

Parameters

dest

New buffer

src

Buffer to copy from

count

Number of characters to copy

Remarks

The memcpy function copies count bytes of src todest. If the source and destination overlap, this function does not ensure that the original source bytes in the overlapping region are copied before being overwritten. Usememmove to handle overlapping regions.

Example

/* MEMCPY.C: Illustrate overlapping copy: memmove
 * handles it correctly; memcpy does not.
 */

#include <memory.h>
#include <string.h>
#include <stdio.h>

char string1[60] = "The quick brown dog jumps over the lazy fox";
char string2[60] = "The quick brown fox jumps over the lazy dog";
/*                           1         2         3         4         5
 *                  12345678901234567890123456789012345678901234567890
 */

void main( void )
{
   printf( "Function:\tmemcpy without overlap\n" );
   printf( "Source:\t\t%s\n", string1 + 40 );
   printf( "Destination:\t%s\n", string1 + 16 );
   memcpy( string1 + 16, string1 + 40, 3 );
   printf( "Result:\t\t%s\n", string1 );
   printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );

   /* Restore string1 to original contents */
   memcpy( string1 + 16, string2 + 40, 3 );

   printf( "Function:\tmemmove with overlap\n" );
   printf( "Source:\t\t%s\n", string2 + 4 );
   printf( "Destination:\t%s\n", string2 + 10 );
   memmove( string2 + 10, string2 + 4, 40 );
   printf( "Result:\t\t%s\n", string2 );
   printf( "Length:\t\t%d characters\n\n", strlen( string2 ) );

   printf( "Function:\tmemcpy with overlap\n" );
   printf( "Source:\t\t%s\n", string1 + 4 );
   printf( "Destination:\t%s\n", string1 + 10 );
   memcpy( string1 + 10, string1 + 4, 40 );
   printf( "Result:\t\t%s\n", string1 );
   printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );
}

Output

Function:   memcpy without overlap
Source:      fox
Destination:   dog jumps over the lazy fox
Result:      The quick brown fox jumps over the lazy fox
Length:      43 characters

Function:   memmove with overlap
Source:      quick brown fox jumps over the lazy dog
Destination:   brown fox jumps over the lazy dog
Result:      The quick quick brown fox jumps over the lazy dog
Length:      49 characters

Function:   memcpy with overlap
Source:      quick brown dog jumps over the lazy fox
Destination:   brown dog jumps over the lazy fox
Result:      The quick quick brown dog jumps over the lazy fox
Length:      49 characters
mem开头的函数基本上都是对内存操作的,它们不管内存里放的是什么数据,只要给出长度,它们就操作。不像strcpy、strcmp等函数一定以'\0'结尾,而且是字符。mem可以操作可见字符、不可见字符、控制字符等,任意数据都可以。
#include "stdafx.h"
#include <stdio.h>
#include <string.h>    // 包含memcpy函数

#define SIZE  10

int main(void)
{
 
    int i = 0;
    char a[10] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09};
    char b[5];

    memcpy(b, a, SIZE);
  
	for (i = 0; i < 5; i++)
	{
            printf("b[%d] = %d\n", i, b[i]);
	}
引用超过数组下标的元素,编译不会报错,但逻辑上就错了。



        int i;
	int a[5] = {1, 2, 3, 4, 5};
	int b[10];
	
	memcpy(b, a, sizeof(int) * 5);
	  
    for (i = 0; i < 10; i++)
	{
		printf("b[%d] = %d\n", i, b[i]);
	}


数组src和dst都是char类型,从数组src复制SIZE个字节到数组dst。但是,两个数组都是整型数组该怎么办?参见下面的例子。
        int i;
	char src[SIZE] = {1, 2, 3, 4, 5, 6, 7, 8 ,9, 10};
	char dst[SIZE];
	
	memcpy(dst, src, SIZE);
	  
	for (i = 0; i < SIZE; i++)
	printf("%-5d ", dst[i]);
		
        printf("\n");
数组src和dst都是int类型,将数组src内容复制到dst数组。前两个参数并不需要使用强制类型转换,因为在函数的原型中,参数的类型是 void *型指针,而任何类型的指针都可以转换为void *型指针。
        int i;
	int src[SIZE] = {1, 2, 3, 4, 5, 6, 7, 8 ,9, 10};
	int dst[SIZE];
	
	memcpy(dst, src, sizeof(int) * SIZE);
	//memcpy(src,dst, sizeof(dst));
	  
	for (i = 0; i < SIZE; i++)
	printf("%-5d ", dst[i]);
		
	printf("\n");


如果数组只有部分内容需要被复制,那么需要复制的数量必须在第3个参数中指明。对于长度大于一个字节的数据,要确保把数量和数据类型的长度相乘。
	int i;
	int src[SIZE] = {1, 2, 3, 4, 5, 6, 7, 8 ,9, 10};
	int dst[SIZE];
	
	memcpy(dst, src, sizeof(int) * 6);
	
	for (i = 0; i < SIZE; i++)
		printf("%-5d ", dst[i]);
    
	printf("\n");
	
	return 0;
}


memset

Sets buffers to a specified character.

void *memset( void *dest, int c, size_tcount );

RoutineRequired HeaderCompatibility
memset<memory.h> or <string.h>ANSI, Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIBSingle thread static library, retail version
LIBCMT.LIBMultithread static library, retail version
MSVCRT.LIBImport library for MSVCRT.DLL, retail version

Return Value

memset returns the value of dest.

Parameters

dest

Pointer to destination

c

Character to set

count

Number of characters

Remarks

The memset function sets the first count bytes of dest to the character c.

Example

#include "stdafx.h"
#include "windows.h"
#include <memory.h>
#include <stdio.h>

void main( void )
{
   DWORD dwbefore,dwafter,dwspand;
   dwbefore = GetTickCount();
   printf("start time:%d\n",dwbefore);
   char buffer[] = "This is a test of the memset function";

   printf( "Before: %s\n", buffer );
   memset( buffer, '*', 4 );
   printf( "After:  %s\n", buffer );
   dwafter = GetTickCount();
   printf("end time:%d\n",dwafter);
   dwspand = dwafter - dwbefore;
   printf("run time:%d\n",dwspand);
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值