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

用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;
}

 

posted @ 2021-07-15 00:21  朱小勇  阅读(368)  评论(0编辑  收藏  举报