-ciox.jpg)
bzero函数
bzero函数位于string.h中可以用于清除字符串,可以用于清除其他类型的内存空间
#include<string.h>
int main(int argc,char** argv){
struct sockaddr_in serveraddr;
bzero(&serveraddr,sizeof(serveraddr));
return 0;
}
案例讲解:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/bitypes.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <signal.h>
int main(int argc, char const *argv[])
{
//创建消息队列key
key_t key = ftok("./fifo",66);
if (-1 == key)
{
printf("creat fifo failed!\n");
}
int shmid = shmget(key,128,IPC_CREAT | 0666);//返回值是共享内存的id
if (-1 == shmid)
{
printf("共享内存创建失败!\n");
}
void *p = shmat(shmid,NULL,0);//返回映射后的内存空间首地址
bzero(p, 128);//bzero相当于memset,可用于将前n个字节清零
char *data = "shmat案例";
memcpy(p,data,strlen(data));
printf("data:%s\n",data);
return 0;
}
上述代码中bzero()用来清除内存映射后的内存空间,防止出现野值。
- 感谢你赐予我前进的力量
赞赏者名单
因为你们的支持让我意识到写文章的价值🙏
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载必须注明来自 卡卡罗特
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果