一、内存操作?
给内存的数据做运算
二、使用步骤
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