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

02_brk/sbrk

时间:2018-09-11 11:30:01      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:locate   nis   stdio.h   include   point   brk   nbsp   erro   ror   

  1 #include<stdio.h>
  2 #include<unistd.h>
  3 main()
  4 {
  5     //situation 1
  6     //int *p = sbrk(4); // Here we allocate 1024(decides to the system) bytes of memory and move(not allocate) 4 bytes forward and then return.
  7     //*p = 8888;
  8     //printf("%d\n",*p);
  9
 10
 11     //situation 2
 12     //int *p = sbrk(0);//Segmentation fault(core dumped)
 13     //*p = 8888;
 14     //printf("%d\n",*p);
 15
 16
 17     //situation 3
 18     //int *p = sbrk(0);
 19     //brk(p+1);
 20     //*p = 8888;
 21     //printf("%d\n",*p);
 22
 23     //situation 4
 24     //int *p = sbrk(4);
 25     //*(p+20) = 100;// You will not get a core dumped here ,but this use is illegal and you may get an unpredictable error.
 26
 27     //situation 5
 28     //int *p = sbrk(0);
 29     //brk(p+4); // Here we
 30     //printf("%d\n",*(p+20)); //0
 31     //*(p+20) = 100;
 32     //printf("%d\n",*(p+20)); //100
 33     //*(p+1023) = 100;
 34     //*(p+1024) = 100; You will get a core dumped
 35
 36     /*
 37     int *p1 = sbrk(4); //Allocate and move
 38     int *p2 = sbrk(4); //move
 39     int *p3 = sbrk(4); //move
 40     int *p4 = sbrk(4); //move
 41     int *p5 = sbrk(4); //move
 42     int *p6 = sbrk(4); //move
 43     int *p7 = sbrk(0); //move
 44     int *p8 = sbrk(0); //move
 45     brk(p8-1);
 46
 47     printf("%p\n",p1);
 48     printf("%p\n",p2);
 49     printf("%p\n",p3);
 50     printf("%p\n",p4);
 51     printf("%p\n",p5);
 52     printf("%p\n",p6);
 53     printf("%p\n",p7);
 54     printf("%p\n",p8);
 55
 56     *p6 = 8888;
 57     *p7 = 9999;
 58     *p8 = 10000;
 59     printf("%d,%d,%d\n",*p6,*p7,*p8);*/
 60
 61
 62
 63     int *p = sbrk(0); //Allocate and return a point(the value is null)
 64     printf("%p\n",p);
 65     brk(p+4);//don‘t move
 66     printf("%p\n",p);
 67     brk(p+4);//don‘t move
 68     printf("%p\n",p);
 69     brk(p+4);//don‘t move
 70     printf("%p\n",p);
 71     brk(p);//don‘t move
 72     printf("%p\n",p);
 73
 74
 75 }

02_brk/sbrk

标签:locate   nis   stdio.h   include   point   brk   nbsp   erro   ror   

原文地址:https://www.cnblogs.com/liujun5319/p/9625418.html

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