MaryLyr.
MaryLyr.

Reputation: 9

exitcode=216 in my program without compiling messages

This is my program. There is no compiler message, but at run‐time it exits. Can anyone please help me? What seems to be wrong?

The problem started after I made it create the file t, so maybe there is something there that I can’t see. Thanks in advance.

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.

Upvotes: 0

Views: 936

Answers (1)

Ken White
Ken White

Reputation: 125708

The problem is that you've declared two dynamic arrays (count and a).

a, count: array of Integer;

Neither of them has memory allocated at this point.

You then allocate memory for a, and get the low and high indexes for a:

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

You then loop through those indexes in the count array, which has no memory allocated yet (you called SetLength on a instead):

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

Accessing memory you haven't yet allocated generates Runtime error 216, which is an access violation (in Delphi, which raises an EAccessViolation if exceptions are enabled) or a general protection fault (in FreePascal).

Upvotes: 1

Related Questions