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);
    }
  • 相关阅读:
    c++vector(入门级)
    端口扫描(TCP)
    推荐安全程序员的书单(系统、网络、安全等)
    My latest news(--2016.12.31)
    HTML+JS+DOM【选项卡自动切换】
    20170916考试总结
    [Usaco2014 Mar]Sabotage
    [SHOI2014]概率充电器
    [Usaco2010 Dec]Exercise 奶牛健美操
    [JZOJ4687]奇袭
  • 原文地址:https://www.cnblogs.com/tszr/p/10968976.html
Copyright © 2011-2022 走看看