linuxc库对内存操作的简单使用

一、内存操作?

给内存的数据做运算

二、使用步骤

1.

代码如下(示例):

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

//字节设置
//#include<string.h>
//void*memset(void*s,int c,size_t n);
//会把s指向区域开始的前n个字节设置为c,并返回s,该函数经常用来清0一块内存.
//memset(s,'\0',256);

//字节比较
//#include<string.h>
//int memcmp(const void*s1,const void*s2,size_t n);
//返回memcmp()会比较s1和s2的前n个字节,如何两块内存相同就返回0

//字节查找
//函数memchr()可以在内存块中查找一个给定的的字节
//#include<string.h>
//void*memchr(const void*s,int c,size_t n);
//函数返回指向第一个匹配c的字节的指针,如果没有找到c,则返回NULL.

//字节移动
//#include<string.h>
//void*memory(void*dst,const void*src,size_t n);


//获取统计信息
//#include <malloc.h>
//struct mallinfo mallinfo(void);
#if 0 
struct mallinfo
{
  int arena; 
  int ordblks;
  int smblks;
  int nblks;
  int hblkhd;
  int usmblks;
  int fmsblks;
  int uordblks;
  int fordblks;
  int keepcost;
};
struct mallinfo m;
m = mallinfo();
#include <malloc.h>
void malloc_stats(void);
#endif 


int main()
{
	char*p;
	char*p2;
	p = (char*)malloc(1024);   //该类型没有强制转换  
	if(!p)
    {
		perror("malloc");
		exit(-1);
	}	
	memset(p,'\0',sizeof(1024));
	
	p2 = (char*)malloc(1024);   //该类型没有强制转换  
	if(!p2)
    {
		perror("malloc");
		exit(-1);
	}	
	memset(p2,'\1',sizeof(1024));	
	if(memcmp(p,p2,1024) == 0)
	{
		printf("两块内存里面是一样的\n");
	}
	
	printf(" ret is %p\n",memchr(p2,'\3',1024));
	
	malloc_stats();
	
	return 0;
}

root@ubuntu:/nfsroot/memory# ./a.out
ret is (nil)
Arena 0:
system bytes = 135168
in use bytes = 2064
Total (incl. mmap):
system bytes = 135168
in use bytes = 2064
max mmap regions = 0
max mmap bytes = 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

今天少内耗10点半睡觉和今天早晚运动

老铁的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值