zoukankan      html  css  js  c++  java
  • Linux_ 内存管理demo

    main1.c

    #include <stdlib.h>
    #include <stdio.h>
    
    int main(void)
    {
        char *buff;
        void *buff2;
    
        buff = malloc(1024);   // ∏≥÷µ ±Ω¯––¡À¿‡–Õ◊™ªª
                                         //µ»Õ¨”⁄:  buff = (char*)malloc(1024);
    
        printf("buff addr is %p 
    ", buff);
    
        sprintf(buff, "hello !
    ");
        printf("buff: %s", buff);
    
        buff2 = malloc(1024);   
        //int x = *buff2;     // ≤ªƒ‹÷±Ω”∂‘void*÷∏’ÎΩ¯––∂¡–¥£¨–Ë“™œ»Ω¯––¿‡–Õ◊™ªª°£
        return 0;
    }
    
    

    main2.c

    #include <stdio.h>
    #include <stdlib.h>
    
    #define MEM_SIZE    (1024*2)    /*UNIT: M*/
    
    int main(void)
    {
        char *buff;
        int count = 0;
    
        while (count++ < MEM_SIZE) {
            buff = (char*)malloc(1024*1024);
            if (buff) {
                sprintf(buff,  "hello");
                printf("malloc %d M bytes memory!
    ", count);
            } else {
                printf("malloc faile!
    ");
                break;
            }
        }
    
        return 0;   
    }
    
    

    main3.c

    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void)
    {
        char *buff;
        int count = 0;
    
        while (1) {
            buff = (char*)malloc(1024*1024);
            if (buff) {
                sprintf(buff,  "hello");
                printf("malloc %d M bytes memory!
    ", ++count);
            } else {
                printf("malloc faile!
    ");
                break;
            }
        }
    
        return 0;   
    }
    
    

    main4.c

    #include <stdlib.h>
    #include <stdio.h>
    
    int main(void)
    {
        char *buff;
        char *p;
    
        buff = (char*)malloc(1024);
        *(buff+1025) = 0;
    
        p = buff + 1025;
        while (1) {
            *p = 0;
            p++;
        }
    
        return 0;
    }
    

    main5.c

    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        char *p = 0;
    
        *p = 1;
    
        return 0;
    }
    

    main6.c

    
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        char * buff;
        char *p;
    
        buff = (char*)malloc(1024);
        p = buff + 1;
    
        //free(buff);
        free(p);        //Ω´≤˙…˙∂Œ¥ÌŒÛ
    
    
        return 0;
    }
    
  • 相关阅读:
    javascript的函数调用什么时候加括号、什么时候不加括号
    妙味——JS学前预热03
    妙味——JS学前预热02
    妙味——JS学前预热01
    springbootday06 mysql
    springboot04 Ajax json Jquery
    springboot02 Thymeleaf
    springbootDay03 cookie和session 购物车技术
    Linux 基本命令
    NodeJs06 高并发
  • 原文地址:https://www.cnblogs.com/Sico2Sico/p/5384220.html
Copyright © 2011-2022 走看看