V
VictorWoo
Unregistered / Unconfirmed
GUEST, unregistred user!
//数组向动态结构体赋值的时候出错,能否指出为什么?
//我想会不会是setlength分配的在栈区,内存不连续了?
//以下代码可以直接拷贝进去执行,谢谢
procedure TForm1.btn1Click(Sender: TObject);
type
TMySubRecord = record //5 bytes
x: array[0..1] of Char;
y: array[0..2] of Char;
end;
TMyRecord = record //2 + 5 * x
a: array[0..1] of Char;
b: array of TMySubRecord
//如果是静态下标的没问题
end;
var
MyArray: array[0..100] of Char;
MySubRecord: TMySubRecord;
MyRecord: TMyRecord;
i: Integer;
begin
SetLength(MyRecord.b, 2)
//把b定义成静态的,不用setlength就没问题
for i := 0 to 99 do
begin
MyArray := Chr(i);
end;
CopyMemory(@MyRecord, @MyArray, 12);
//动态分配的,执行到这里溢出
Caption := 'Done';
end;
//我想会不会是setlength分配的在栈区,内存不连续了?
//以下代码可以直接拷贝进去执行,谢谢
procedure TForm1.btn1Click(Sender: TObject);
type
TMySubRecord = record //5 bytes
x: array[0..1] of Char;
y: array[0..2] of Char;
end;
TMyRecord = record //2 + 5 * x
a: array[0..1] of Char;
b: array of TMySubRecord
//如果是静态下标的没问题
end;
var
MyArray: array[0..100] of Char;
MySubRecord: TMySubRecord;
MyRecord: TMyRecord;
i: Integer;
begin
SetLength(MyRecord.b, 2)
//把b定义成静态的,不用setlength就没问题
for i := 0 to 99 do
begin
MyArray := Chr(i);
end;
CopyMemory(@MyRecord, @MyArray, 12);
//动态分配的,执行到这里溢出
Caption := 'Done';
end;