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 \n", buff);

    sprintf(buff, "hello !\n");
    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!\n", count);
        } else {
            printf("malloc faile!\n");
            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!\n", ++count);
        } else {
            printf("malloc faile!\n");
            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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值