码迷,mamicode.com
首页 > 其他好文 > 详细

关于防止内存泄漏的封包和解包

时间:2017-10-18 23:10:28      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:pac   http   fclose   next   数据类型   sem   return   ext   temp   

link.h文件

 1 #ifndef __LINK_H
 2 #define __LINK_H
 3 #include<stdio.h>
 4 #include<stdlib.h>
 5 #include<string.h>
 6 #include<unistd.h>
 7 struct DATA
 8 {
 9     int id;
10     char name[20];
11 };
12 typedef struct student
13 {
14     struct DATA data;
15     struct student *next;
16 }STU,*Pstu;
17 
18 
19 Pstu create_link(Pstu head);
20 void show_link(Pstu head);
21 Pstu recv_link(Pstu head,struct DATA *data);
22 
23 void * mymalloc(int size,int line,const char *file,const char *func);
24 void myfree(void *ptr,int line,const char *file,const char *func);
25 void free_link(Pstu head);
26 #endif

link.c文件

 1 #include"link.h"
 2 
 3 void * mymalloc(int size,int line,const char *file,const char *func)
 4 {
 5     void * p=(void *)malloc(size);
 6     FILE * fp;
 7     fp=fopen("./malloc","a");
 8     fprintf(fp,"%p\t%d\t%s\t%s\t%d\n",p,line,file,func,size);
 9     fclose(fp);
10     return p;
11 }
12 
13 void myfree(void *ptr,int line,const char *file,const char *func)
14 {
15     FILE *fp;
16     fp=fopen("./free","a");
17     fprintf(fp,"%p\t%d\t%s\t%s\t\n",ptr,line,file,func);
18     fclose(fp);
19     free(ptr);
20     return ;
21 }
22 
23 
24 Pstu create_link(Pstu head)
25 {
26     Pstu temp=(Pstu)mymalloc(sizeof(STU),__LINE__,__FILE__,__func__);
27     printf("please input id:");
28     scanf("%d",&temp->data.id);
29     printf("please input name:");
30     scanf("%s",temp->data.name);
31     temp->next=head;
32     return temp;
33     return temp;
34 }
35 
36 void show_link(Pstu head)
37 {
38     printf("id\tname\n");
39     while(head!=NULL)
40     {
41         printf("%d\t%s\n",head->data.id,head->data.name);
42         head=head->next;
43     }
44 }
45 
46 Pstu recv_link(Pstu head,struct DATA *data)
47 {
48 
49     Pstu temp=(Pstu)mymalloc(sizeof(STU),__LINE__,__FILE__,__func__);
50     temp->data=*data;
51     temp->next=head;
52     myfree(data,__LINE__,__FILE__,__func__);
53     return temp;
54 }
55 
56 void free_link(Pstu head)
57 {
58     Pstu p=NULL;
59     while(head!=NULL)
60     {
61         p=head->next;
62         myfree(head,__LINE__,__FILE__,__func__);
63         head=p;
64     }
65 }

pack.h文件

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 #include<unistd.h>
 5 #include<sys/types.h>
 6 #include<sys/stat.h>
 7 #include<fcntl.h>
 8 struct pack
 9 {
10     int ver;
11     int type;
12     long len;
13     char buf[0];
14 };  
15 void mypack(int fd,int ver,int type,void * data);
16 void *MyUnpack(int fd,int *len,int *type); 

pack.c文件

 1 #include"pack.h"
 2 #include"link.h"
 3 //封包
 4 void   mypack(int fd,int ver,int type,void * data)
 5 {
 6     struct DATA *p=(struct DATA*)data;
 7 
 8     struct pack *temp;
 9     temp=(struct pack *)mymalloc(sizeof(struct pack)+sizeof(struct DATA),__L
10 INE__,__FILE__,__func__);
11 
12     temp->ver=ver;
13     temp->type=type;
14     temp->len=sizeof(struct DATA);
15     memcpy(temp->buf,p,temp->len);
16 
17     int ret=write(fd,temp,sizeof(struct pack)+temp->len);
18     printf("ret=%d\n",ret);
19     myfree(temp,__LINE__,__FILE__,__func__);
20 }
21 //解包
22 void *MyUnpack(int fd,int *len,int *type)
23 {
24 
25     struct pack temp;
26     read(fd,&temp,sizeof(temp));
27     void *p=(void
28 *)mymalloc(temp.len,__LINE__,__FILE__,__func__);
29     read(fd,p,temp.len);
30     *len=temp.len;
31     *type=temp.type;
32     return p;
33 }

p1.c文件

 1 #include <sys/types.h>
 2 #include <sys/ipc.h>
 3 #include <sys/sem.h>
 4 #include<stdio.h>
 5 #include<sys/shm.h>
 6 #include<stdlib.h>
 7 #include"pack.h"
 8 #include"link.h"
 9 int main()
10 {
11     //int mkfifo(const char *pathname, mode_t mode);
12     mkfifo("./test",0777);
13     int fd=open("./test",O_WRONLY);
14     Pstu head=NULL;
15     for(int i=0;i<5;i++)
16     {
17         head=create_link(head);
18         mypack(fd,1,2,&head->data);
19     }
20     show_link(head);
21     free_link(head);
22     return 0;
23 }

v1.c文件

 1 #include <sys/types.h>
 2 #include <sys/ipc.h>
 3 #include <sys/sem.h>
 4 #include<stdio.h>
 5 #include<sys/shm.h>
 6 #include"pack.h"
 7 #include"link.h"
 8 int main()
 9 {
10     mkfifo("./test",0777);
11 
12     int fd=open("./test",O_RDONLY);
13     int len, type;
14     Pstu head=NULL;
15 
16     void  *buf;
17     for(int i=0;i<5;i++)
18     {
19         buf=MyUnpack(fd,&len,&type);
20         switch(type)
21         {
22             case 1://代表数据类型是字符串
23                 {
24                     printf("on case 1\n");
25                     puts((char *)buf);
26                 }
27                 break;
28             case 2://代表struct DATA
29                 head=recv_link(head,buf);
30                 break;
31         }
32     }
33     show_link(head);
34     free_link(head);
35     printf("end of v\n");
36 }   

Makefile

1 ALL:
2     gcc pack.c link.c v1.c -o v
3     gcc pack.c link.c p1.c -o p
4 clean:
5     rm -rf malloc free

make之后运行./p和./v效果如下:

技术分享

然后查看malloc和free效果如下:

技术分享

关于防止内存泄漏的封包和解包

标签:pac   http   fclose   next   数据类型   sem   return   ext   temp   

原文地址:http://www.cnblogs.com/ysjd/p/7689429.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!