• 吴裕雄--天生自然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);
    }
  • 相关阅读:
    Delphi编写星光效果
    网格动画
    在窗体边框上画图
    algebra单元
    CMOS单元
    类似于Split(VB)的函数
    利用PHPLIB加入模板功能
    随机产生一个中文
    测试PHP
    获得指定后缀名的文件数
  • 原文地址:https://www.cnblogs.com/tszr/p/10968976.html
Copyright © 2020-2023  润新知