• 采用malloc分别分配2KB个人空间,然后,realloc调整到6KB、1MB、3MB、10MB场地,分别这五内存“A”、“B”、“C”、“D”、“E”灌装


    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<malloc.h>
    int main(void)
    {
        char *str1 = NULL;
        char *str2 = NULL;
        char *str3 = NULL;
        char *str4 = NULL;
        char *str5 = NULL;
        str1 = (char*)malloc(2*1024*sizeof(char));
        if(str1==NULL)
        {
            printf("malloc error! ");
            return -1;
        }
        printf("malloc  2KB: %p ",str1);
        memset(str1,'A',2*1024*sizeof(char));
        printf("mem content:%s ",str1);
        str2 = (char*)realloc(str1,6*1024*sizeof(char));
        if(str2==NULL)
        {
            printf("realloc error! ");
            return -1;
        }
        printf("realloc 6KB: %p ",str2);
        memset(str2,'B',6*1024*sizeof(char));
        printf("mem content:%s ",str2);
        str3 = (char*)realloc(str2,1024*1024*sizeof(char));
        if(str3==NULL)
        {
            printf("realloc error! ");
            return -1;
        }
        printf("realloc 1MB: %p ",str3);
        memset(str3,'C',1024*1024*sizeof(char));
        printf("mem content:%s ",str3);
        str4 = (char*)realloc(str3,3*1024*1024*sizeof(char));
        if(str4==NULL)
        {
            printf("realloc error! ");
            return -1;
        }
        printf("realloc 3MB: %p ",str4);
        memset(str4,'D',3*1024*1024*sizeof(char));
        printf("mem content:%s ",str4);
        str5 = (char*)realloc(str4,10*1024*1024*sizeof(char));
        if(str5==NULL)
        {
            printf("realloc error! ");
            return -1;
        }
        printf("realloc 10MB: %p ",str5);
        memset(str5,'E',10*1024*1024*sizeof(char));
        printf("mem content:%s ",str5);
        free(str5);
        return 0;
    }

     

    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    P4342 [IOI1998]Polygon
    P1194 买礼物
    P1363 幻想迷宫
    Installing Wine 1.5: configure: error: Cannot build a 32-bit program, you need to install 32-bit development libraries(转载)
    Linux系统调用之open(), close() (转载)
    undefined reference to 'pthread_create'问题解决(转载)
    linux中的C里面使用pthread_mutex_t锁(转载)
    #if、#ifdef、#if defined之间的区别(转载)
    linux下解压tgz文件(转载)
    linux修改用户主目录的方法 (转载)
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4776159.html
Copyright © 2020-2023  润新知