zoukankan      html  css  js  c++  java
  • 动态内存

    在c语言中是malloc

      用法:#include

      或#include<stdlib.h>

      功能:分配长度为num_bytes字节的内存块

      说明:如果分配成功则返回指向被分配内存的指针,否则返回空指针NULL

      当内存不再使用时,应使用free()函数将内存块释放。

      malloc的语法是:指针名=(数据类型*)malloc(长度),(数据类型*)表示指针.

    用free()释放内存

      举例

      // malloc.c

      #include

      #include

      main()

      {

      char *p;

      clrscr(); // clear screen

      p=(char *)malloc(100);

      if(p)

      printf("Memory Allocated at: %x",p);

      else

      printf("Not Enough Memory! ");

      

      if(p)

      free(p);

      getchar();

      return 0;

      }

    在c++中是new;

    用delete()释放内存

    #include"iostream"
    using namespace std;
    struct aaa
    {
     int a;
     int c;
    };
    int main()
    {
     aaa * fp = new aaa[10];//申请内存的大小
     fp->a = 10;//因为动态申请的内存没有名字,也不知道其地址,但它确实是存在的,通过指针来访问。
     fp->c=20;
     cout << fp->a << "," << fp->c << endl;
    }

  • 相关阅读:
    RocketMQ Message hasn't been sent. Caused by No route info of this topic, test-topic
    Barrier
    WPF之资源
    WPF之命令
    WPF之事件
    WPF之属性
    多路Binding
    Binding的数据转换
    Binding的数据校验
    为Binding指定源的方法
  • 原文地址:https://www.cnblogs.com/maodun/p/6171000.html
Copyright © 2011-2022 走看看