- //---------------------------------------------------------------------------
- #include <vcl.h>
- #include <stdio.h>
- #include <alloc.h>
- #pragma hdrstop
- #include "Unit1.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- #pragma resource "*.dfm"
- TForm1 *Form1;
- //---------------------------------------------------------------------------
- __fastcall TForm1::TForm1(TComponent* Owner)
- : TForm(Owner)
- {
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::Button1Click(TObject *Sender)
- {
- char *str;
- int sz;
- sz=1000000;
- if ((str = (char *) malloc(sz)) == NULL)
- {
- ShowMessage("没有足够的内存!");
- }
- else
- {
- str[0]='1';
- str[1]='2';
- str[2]='3';
- str[3]='4';
- str[4]=0;
- ShowMessage("已分配内存!");
- }
- free(str);
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::Button2Click(TObject *Sender)
- {
- int *str;
- int sz;
- sz=10000;
- if ((str = (int *) malloc(sz)) == NULL)
- {
- ShowMessage("没有足够的内存!");
- }
- else
- {
- str[0]=12345678;
- str[1]=789345612;
- Edit1->Text=IntToStr(str[0]);
- ShowMessage("已分配内存!");
- }
- free(str);
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::Button3Click(TObject *Sender)
- {
- char *str;
- str = (char *) malloc(100000);
- strcpy(str, "Hello");
- sprintf(str,"地址为:%p",str);
- ShowMessage(str);
- str = (char *) realloc(str, 200000);
- sprintf(str,"地址为:%p",str);
- ShowMessage(str);
- free(str);
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::Button4Click(TObject *Sender)
- {
- int *dd;
- dd=new int[1000];
- if(dd==NULL)
- {
- ShowMessage("无足够内存!");
- return;
- }
- dd[0]=123456789;
- dd[1]=678954321;
- delete dd;
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::Button5Click(TObject *Sender)
- {
- char *dd;
- dd=new char[1000];
- if(dd==NULL)
- {
- ShowMessage("无足够内存!");
- return;
- }
- dd[0]='1';
- dd[1]='6';
- dd[2]=0;
- delete dd;
- }
- //---------------------------------------------------------------------------