C++源码:C++语言中的内存动态分配
// C++ : Example for (new and delete)
// C++中的动态内存分配
#include <iostream>
#include <malloc.h>
using namespace std;
void fun1();
main()
{
fun1();
}
void fun1()
{
int *i=new(int); //申请内存 用 new
const int w11=123;
*i=10;
cout<<"\n\n You are Wellcome ! C++中的动态内存分配 \n\n\n";
cout<<"C+++: i="<<i<<"\n";
cout<<"C+++: *i="<<*i<<"\n";
cout<<"C+++: CONST W11= "<<w11<<"\n";
cout<<"C+++: Size of INT: "<<sizeof(int);
cout<<"\nC+++: Size of Char: "<<sizeof(char);
cout<<"\nC+++: Size of long :"<<sizeof(long);
cout<<"\n\n End. \n\n";
free(i); //注意:对于C++对象,释放其所占内存 用 delete !
}
赞 (0)