在我的程序中退出代码=216,无需编译消息[关闭]

问题描述 投票:0回答:1

这是我的程序。没有编译器消息,但在运行时它退出。有人可以帮我吗?好像出了什么问题?

问题是在我创建文件后开始的

t
,所以也许有一些我看不到的东西。预先感谢。

program MyProgr;

var
  F: text;
  t: Textfile;
  a, count: array of Integer;
  b: Integer;
  i, int: Integer;
  countnums: Integer;
  n, m: String;
  lin, nums: Integer;
  Small, Big: Integer;

procedure DoWhatEver(S: string);
begin
  val(S, int);
  Write(S, '     ');
  for i := Small to Big do
    if (a[i] = int) then
      count[i] := count[i] + 1;
end;

procedure FilltheArray;
begin
  for i := Small to Big do
    a[i] := i + 1;
end;

procedure ProcessString;
var
  Strng, S: string;
  Last, P: Integer;
begin
  readln(F, Strng);
  Last := 0;
  while Last < length(Strng) do
  begin
    P := Last + 1;
    while (P <= length(Strng)) and (Strng[P] <> ' ') do
      inc(P);
    S := copy(Strng, Last + 1, (P - Last - 1));
    DoWhatEver(S);
    Last := P;
  end
end;

procedure ProcessStringA;
var
  Strng: string;
  Last, P: Integer;
begin
  readln(F, Strng);
  Last := 0;
  while Last < length(Strng) do
  begin
    P := Last + 1;
    while (P <= length(Strng)) and (Strng[P] <> ' ') do
      inc(P);
    n := copy(Strng, Last + 1, (P - Last - 1));
    val(n, nums);
    Last := P;
  end
end;

procedure ProcessStringB;
var
  Strng: string;
  Last, P: Integer;
begin
  readln(F, Strng);
  Last := 0;
  while Last < length(Strng) do
  begin
    P := Last + 1;
    while (P <= length(Strng)) and (Strng[P] <> ' ') do
      inc(P);
    m := copy(Strng, Last + 1, (P - Last - 1));
    val(m, lin);
    Last := P;
  end
end;

begin
  assign(F, 'myfile.txt');
  reset(F);
  ProcessStringA;
  Writeln(nums);
  ProcessStringB;
  Writeln(lin);
  setlength(a, nums);
  Small := Low(a);
  Big := High(a);
  for i := Small to Big do
    count[i] := 0;
  FilltheArray;
  while not eof(F) do
    ProcessString;

  for i := Small to Big do
  begin
    if count[i] = 2 then
      countnums := countnums + 1;
  end;
  Close(F);
  assign(t, 'fileout.txt');
  Rewrite(t);
  Writeln(t, countnums);
  Close(t);

end.
freepascal
1个回答
1
投票

问题是您声明了两个动态数组(

count
a
)。

a, count: array of Integer;

此时它们都没有分配内存。

然后为

a
分配内存,并获取
a
的低索引和高索引:

setlength(a, nums);
Small := Low(a);
Big := High(a);

然后循环遍历

count
数组中的这些索引,该数组尚未分配内存(您在
SetLength
上调用
a
):

for i := Small to Big do
  count[i] := 0;

访问尚未分配的内存会生成

Runtime error 216
,这是一种访问冲突(在 Delphi 中,如果启用了异常,则会引发
EAccessViolation
)或一般保护错误(在 FreePascal 中)。

© www.soinside.com 2019 - 2024. All rights reserved.