您的位置:首页 > 博客中心 > 编程语言 >

C语言动态分配内存及回收

时间:2022-03-29 01:57

用malloc和free;类似与C++的new和delete

代码:

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char* argv[])
{
    void* ptr = (void*)malloc(100);
    void* tPtr = (void*)malloc(200);
    cout<<&ptr<<endl;       //0x61fe18
    cout<<&tPtr<<endl;      //0x61fe10
    cout<<ptr<<endl;        //0xeb1ac0
    cout<<tPtr<<endl;       //0xeb5cc0

    free(ptr);
    free(tPtr);    
    return 0;
}

 

本类排行

今日推荐

热门手游