zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然C语言开发:内存管理

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    int main()
    {
       char name[100];
       char *description;
     
       strcpy(name, "Zara Ali");
     
       /* 动态分配内存 */
       description = (char *)malloc( 200 * sizeof(char) );
       if( description == NULL )
       {
          fprintf(stderr, "Error - unable to allocate required memory
    ");
       }
       else
       {
          strcpy( description, "Zara ali a DPS student in class 10th");
       }
       printf("Name = %s
    ", name );
       printf("Description: %s
    ", description );
    }
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    int main()
    {
       char name[100];
       char *description;
     
       strcpy(name, "Zara Ali");
     
       /* 动态分配内存 */
       description = (char *)malloc( 30 * sizeof(char) );
       if( description == NULL )
       {
          fprintf(stderr, "Error - unable to allocate required memory
    ");
       }
       else
       {
          strcpy( description, "Zara ali a DPS student.");
       }
       /* 假设您想要存储更大的描述信息 */
       description = (char *) realloc( description, 100 * sizeof(char) );
       if( description == NULL )
       {
          fprintf(stderr, "Error - unable to allocate required memory
    ");
       }
       else
       {
          strcat( description, "She is in class 10th");
       }
       
       printf("Name = %s
    ", name );
       printf("Description: %s
    ", description );
     
       /* 使用 free() 函数释放内存 */
       free(description);
    }
  • 相关阅读:
    pandas--对axis=0,axis=1的理解
    启动secondarynamenode时报错
    5月27日经历问题(在有框架的情况下从无到有增加一套功能)
    5.21工作记录(修改页面跳转,去掉多余的js;增加图片清除功能)
    工作记录520
    5月14日经历问题
    idea快捷键
    Linux下常用redis指令
    初识lunix
    Redis
  • 原文地址:https://www.cnblogs.com/tszr/p/10968976.html
Copyright © 2011-2022 走看看