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;
    }
    
  • 相关阅读:
    shell编程基础进阶
    Ansible自动化配置详解
    sersync实时同步实战
    NFS网络文件系统详解
    CentOS7下rsync服务的基本详解和使用
    CentOS7版本基础使用
    网络基础-交换机、路由器、OSI7层模型
    linux-sed命令
    Cause: java. lang.InstantiationException: tk.mybatis.mapper.provider.base.BaseInsertProvider
    Spring Boot MyBatis注解:@MapperScan和@Mapper
  • 原文地址:https://www.cnblogs.com/Sico2Sico/p/5384220.html
Copyright © 2011-2022 走看看