发信人: lofe ()感激生活(), 信区: BorlandDev
标 题: .Delphi中动态申请内存
发信站: 哈工大紫丁香 (Mon Sep 4 16:13:41 2000), 转信
Let's say your data structure looks like this:
type
TMyStructure = record
Name: String[40];
Data: array[0..4095] of Integer;
end;
That's too large to be allocated globally, so instead of
declaring a global variable,
var
MyData: TMyStructure;
you declare a pointer type,
type
PMyStructure = ^TMyStructure;
and a variable of that type,
var
MyDataPtr: PMyStructure;
Such a pointer consumes only four bytes of the data segment.
Before you can use the data structure, you have to allocate it
on the heap:
New(MyDataPtr);
and now you can access it just like you would global data. The
only difference is that you have to use the caret operator to
dereference the pointer:
MyDataPtr^.Name := 'Lloyd Linklater';
MyDataPtr^.Data[0] := 12345;
Finally, after you're done using the memory, you deallocate it:
Dispose(MyDataPtr);
--
※ 修改:.haojs 于 Sep 4 16:11:13 修改本文.[FROM: bbs.hit.edu.cn]
--
※ 转寄:.武汉白云黄鹤站 bbs.whnet.edu.cn.[FROM: bbs.hit.edu.cn]
--
☆ 来源:.哈工大紫丁香 bbs.hit.edu.cn.[FROM: haojs.bbs@bbs.whnet.]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:6.470毫秒