为什么此记录结构正在编译但出现运行时错误

时间:2019-06-19 18:07:15

标签: record freepascal

我正在尝试下面的代码,其中包含简单的字符串和整数记录:

program Progrecords; 

Type  
    Progrec = Record  
        pname : string;  
        pnum : integer; 
        end;  

var 
    Progs : Array of Progrec; 
    aprog, pp: Progrec; 

begin 
    aprog.pname := 'abc';
    aprog.pnum := 4; 
    Progs[0] := aprog;

    aprog.pname := 'def';
    aprog.pnum := 6; 
    Progs[1] := aprog;

    for pp in Progs do begin
        writeln('Name: ',pp.pname, '; Num: ', pp.pnum); 
    end;
end. 

它可以编译,但会给出运行时错误:

$ fpc rnrecords.pas && ./rnrecords
Free Pascal Compiler version 3.0.0+dfsg-11+deb9u1 [2017/06/10] for x86_64
Copyright (c) 1993-2015 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling rnrecords.pas
rnrecords.pas(17,2) Warning: Variable "Progs" of a managed type does not seem to be initialized
Linking rnrecords
/usr/bin/ld.bfd: warning: link.res contains output sections; did you forget -T?
27 lines compiled, 0.1 sec
1 warning(s) issued

Runtime error 216 at $0000000000400226
  $0000000000400226
  $000000000040018C

我正在使用fpc版本3.0.0的Debian Stable Linux

问题出在哪里,如何解决?感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

Progs是一个动态数组,必须在使用前分配。

例如:

SetLength(Progs,2);  // Allocates two records

请参见Dynamic Array