[英]How to allocate memory to pointer to a record? Delphi 7
I have this type: 我有这种类型:
type
pTRegex_sec=^TRegex_sec;
TRegex_sec = record
secs: Array of pTRegex_sec;
len: byte;
hasSections: boolean;
hasUnits: boolean;
units: Array of TRegex_unit;
end;
type TRegex_assertions = record
len: byte;
secs: Array of TRegex_sec;
end;
I would like to allocate memory for the type TRegex_sec: 我想为TRegex_sec类型分配内存:
var Assertions: TRegex_assertions;
begin
setlength(Assertions.secs, 1);
GetMem(Assertions.secs[0], SizeOf(TRegex_sec));
end;
The error I have is "Incompatible types": Assertions.secs[0]<-- here
我遇到的错误是“不兼容的类型”:
Assertions.secs[0]<-- here
Another attempt, same error: 另一个尝试,同样的错误:
New(Assertions.secs[0]);
How to do it correctly? 如何正确做?
The TRegex_assertions.secs
field is a dynamic array of records , not an array of pointers . TRegex_assertions.secs
字段是记录的动态数组,而不是指针数组。 There is no need to use GetMem()
to allocate that array, SetLength()
already handles that for you. 无需使用
GetMem()
分配该数组, SetLength()
已经为您处理了该数组。
However, the TRegex_sec.secs
field is a dynamic array of pointers . 但是,
TRegex_sec.secs
字段是指针的动态数组。 Use SetLength()
to allocate that array as needed, and then use New()
to allocate individual TRegex_sec
instances to populate it with: 使用
SetLength()
根据需要分配该数组,然后使用New()
分配单个TRegex_sec
实例,以使用以下方法填充该TRegex_sec
:
var
Assertions: TRegex_assertions;
begin
SetLength(Assertions.secs, 1);
SetLength(Assertions.secs[0].secs, 1);
New(Assertions.secs[0].secs[0]);
...
end;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.