内存操作函数

#include <stdlib.h>

void *	 memchr (const void *block, int c, size_t size);
int 	 memcmp (const void *a1, const void *a2, size_t size);
void *	 memcpy (void *restrict to, const void *restrict from, size_t size);
void *	 memmove (void *to, const void *from, size_t size);
void *	 memset (void *block, int c, size_t size);

1、memchr

void *	 memchr (const void *block, int c, size_t size);

Description:
This function finds the first occurrence of the byte c (converted to an unsigned char) in the initial size 
 bytes of the object beginning at block. The return value is a pointer to the located byte, or a null 
 pointer if no match was found. 

2、memcmp

int 	 memcmp (const void *a1, const void *a2, size_t size);

Description:
The function memcmp compares the size bytes of memory beginning at a1 against the size bytes of 
 memory beginning at a2. The value returned has the same sign as the difference between the first 
 differing pair of bytes (interpreted as unsigned char objects, then promoted to int). 

If the contents of the two blocks are equal, memcmp returns 0. 

3、memcpy

void *	 memcpy (void *restrict to, const void *restrict from, size_t size);

Description:
The memcpy function copies size bytes from the object beginning at from into the object beginning 
 at to. The behavior of this function is undefined if the two arrays to and from overlap; use memmove 
 instead if overlapping is possible. 

The value returned by memcpy is the value of to. 

Here is an example of how you might use memcpy to copy the contents of an array: 



         struct foo *oldarray, *newarray;

         int arraysize;

         ...

         memcpy (new, old, arraysize * sizeof (struct foo));

e.g.

/*拷贝数组*/
unsigned char bufA[BUF_LEN];
unsigned char bufB[BUF_LEN];
memcpy(bufA, bufB, BUF_LEN - 1); /* 预留最后一位给空字符'\0' */

4、memmove

void *	 memmove (void *to, const void *from, size_t size);

Description:
memmove copies the size bytes at from into the size bytes at to, even if those two blocks of space 
 overlap. In the case of overlap, memmove is careful to copy the original values of the bytes in the 
 block at from, including those bytes which also belong to the block at to. 

The value returned by memmove is the value of to.

5、memset

void *	 memset (void *block, int c, size_t size);

Description:
This function copies the value of c (converted to an unsigned char) into each of the first size bytes of 
 the object beginning at block. It returns the value of block. 

e.g.

/*清空数组*/
unsigned char buff[BUFF_LEN];
memset(buff, 0, BUFF_LEN);

/*清空结构体*/
struct IP_Packet ip_pkt;
memset(&ip_pkt, 0, sizeof(IP_Packet));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值