美文网首页
内存分配

内存分配

作者: 滩主 | 来源:发表于2020-06-07 11:55 被阅读0次

    观察brk和mmap
    cat /proc/$pid/maps

    #include <unistd.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    int main()
    {
        char* p0 = sbrk(0);
        //*p0 = 65;
    
        malloc(1);
        char* p1 = sbrk(0);
        printf("%d %p %p\n",p1-p0,p1,p0);
        malloc(1);
        char* p2 = sbrk(0);
        printf("%d %p %p\n",p2-p0,p2,p0);
    
    
        //*(p0+1024*136-1) = 65;
        //*(p0+1024*136) = 65;
    
        char* s = malloc(1024*256);
        char* p3 = sbrk(0);
        printf("%d %p %p %p\n",p3-p0,p3,p0,s);
        // s[1024*260] = 65;
        s[1024*260-16-1] = 65;
    
        sleep(500);
        //s[1024*1024-1] = 65;
        //char* p4 = sbrk(0);
        //printf("%d %p %p\n",p4-p0,p4,p0);
    
    }
    

    相关文章

      网友评论

          本文标题:内存分配

          本文链接:https://www.haomeiwen.com/subject/uifctktx.html