I was implementing adjacency list in Pascal (by first reading edge end points, and then using dynamic arrays to assign required amount of memory to edgelist of each node). The program executes fine, gives correct outputs but gives runtime error 216 just before exiting.
The code is :
type aptr = array of longint;
var edgebuf:array[1..200000,1..2] of longint;
ptrs:array[1..100000] of longint;
i,j,n,m:longint;
elist:array[1..100000] of aptr;
{main}
begin
readln(n,m);
fillchar(ptrs,sizeof(ptrs),#0);
for i:=1 to m do begin
readln(edgebuf[i][1],edgebuf[i][2]);
inc(ptrs[edgebuf[i][1]]);
end;
for i:=1 to n do begin
setlength(elist[i],ptrs[i]);
end;
fillchar(ptrs,sizeof(ptrs),#0);
for i:=1 to m do begin
inc(ptrs[edgebuf[i][1]]);
elist[edgebuf[i][1]][ptrs[edgebuf[i][1]]]:=edgebuf[i][2];
end;
for i:=1 to n do begin
writeln(i,' begins');
for j:=1 to ptrs[i] do begin
write(j,' ',elist[i][j],' ');
end;
writeln();
writeln(i,' ends');
end;
writeln('bye');
end.
When run on file
4 5
1 2
3 2
4 3
2 1
2 3
gives output:
1 begins
1 2
1 ends
2 begins
1 1 2 3
2 ends
3 begins
1 2
3 ends
4 begins
1 3
4 ends
bye
Runtime error 216 at $0000000000416644
$0000000000416644
$00000000004138FB
$0000000000413740
$0000000000400645
$00000000004145D2
$0000000000400180
Once the program says «bye», what is the program executing that is giving runtime error 216?
Сообщение от вячеслав92
что делать?
Исправлять ошибку, что еще? 216 — это GPF (General Protection Fault), либо ты в программе разыменовываешь NIL, либо вылетаешь за границы массивов (тотальная привычка отключать {$R} еще до того, как программа отлажена, в надежде уменьшить размер EXE на несколько байт — вполне частая причина подобного поведения). Включи все опции контроля (Options->Compiler->Generated code, всё что в левом столбце). Заодно можешь в поле «Additional compiler args» написать -gh. Пересобери программу и запусти ее, посмотри, что теперь выдает.
program sortirovki;
{$s-}
{$r-}
type
list=^zveno;
zveno=record
c:byte;
next:list
end;
elem=record
sgn:boolean;
n:integer;
zero:boolean;
ch:list;
kz:integer
end;
structure=array [1..1500] of elem;
var
B:structure;
i,N,hs,M,l,prc,src,prh,srh,T:longint;
c:char;
p:list;
vsgn:boolean;
function compare(var x,y:elem):boolean; {true, esli x>y}
var px,py:list;
fl:boolean;
begin
if x.sgn <> y.sgn then
if x.sgn=false then compare:=true else compare:=false else
if x.n<>y.n then
if x.n < y.n then
if x.sgn=false then compare:=false else compare:=true else
if x.sgn=false then compare:=true else compare:=false else
begin
new(px);
new(py);
px:=x.ch;
py:=y.ch;
if x.ch^.c=0 then
begin
repeat
x.ch:=x.ch^.next;
until x.ch^.c<>0;
end;
if y.ch^.c=0 then
begin
repeat
y.ch:=y.ch^.next;
until y.ch^.c<>0;
end;
if x.ch^.c < y.ch^.c then fl:=false else fl:=true;
while (x.ch^.c = y.ch^.c) and (x.ch<>nil) and (y.ch<>nil) do
begin
x.ch:=x.ch^.next;
y.ch:=y.ch^.next;
if x.ch^.c < y.ch^.c then fl:=false else fl:=true;
end;
x.ch:=px;
y.ch:=py;
if (x.sgn=false) and fl then compare:=true else
if (x.sgn=false) and not fl then compare:=false else
if (x.sgn=true) and fl then compare:=false else compare:=true;
end;
srh:=srh+1;
src:=src+1;
end;
procedure swap(var x,y:elem);
var z:elem;
begin
new(z.ch);
z.n:=x.n;
z.kz:=x.kz;
z.sgn:=x.sgn;
z.zero:=x.zero;
z.ch:=x.ch;
x.n:=y.n;
x.kz:=y.kz;
x.sgn:=y.sgn;
x.zero:=y.zero;
x.ch:=y.ch;
y.n:=z.n;
y.kz:=z.kz;
y.sgn:=z.sgn;
y.zero:=z.zero;
y.ch:=z.ch;
prh:=prh+1;
prc:=prc+1;
end;
procedure heapify(var B:structure;j:integer);
var max:integer;
begin
if (2*j <= hs) and (not compare(B[j],B[2*j])) then max:=2*j else max:=j;
if (2*j+1 <= hs) and (not compare(B[max],B[2*j+1])) then max:=2*j+1;
if j <> max then begin
swap(B[j],B[max]);
heapify(B,max);
end
end;
procedure heapsort(var B:structure);
var i,l:integer;
begin
for i:=hs div 2 downto 1 do
heapify(B,i);
{for l:=1 to N do
write(a[l],’ ‘);}
for i:=hs downto 2 do
begin
swap(B[1],B[i]);
dec(hs);
heapify(B,1);
end;
end;
procedure chsort(var B:structure);
var i,j:integer;
begin
i:=1;
while i<N do
begin
if compare(B[i],B[i+1]) then
begin
j:=i;
while (not compare(B[j+1],B[j])) and (j <> 0) do
begin
swap(B[j],B[j+1]);
dec(j);
end;
inc(i);
end else inc(i);
end;
end;
function vvod(var B:structure):string;
var s:char;
fl:boolean;
i:integer;
p:list;
cp:boolean;
begin
i:=0;
repeat
inc(i);
fl:=true;
read(s);
if (s<>’ ‘) and (s<>’.’) then
begin
if s=’-‘ then begin read(s);B[i].sgn:=true; end else B[i].sgn:=false;
if (s=’0′) and fl then B[i].kz:=B[i].kz+1 else
begin
B[i].n:=B[i].n+1;
fl:=false
end;
if s<>’0′ then B[i].zero:=false else B[i].zero:=true;
new(B[i].ch);
B[i].ch^.c:=ord(s)-ord(‘0’);
B[i].ch^.next:=nil;
new(p);
p:=B[i].ch;
repeat
read(s);
if (s<>’ ‘) and (s<>’.’) then
begin
if s=’0′ then B[i].zero:=B[i].zero and true else B[i].zero:=false;
if (s=’0′) and fl then B[i].kz:=B[i].kz+1
else
begin
B[i].n:=B[i].n+1;
fl:=false
end;
new(B[i].ch^.next);
B[i].ch:=B[i].ch^.next;
B[i].ch^.c:=ord(s)-ord(‘0’);
B[i].ch^.next:=nil
end;
until (s=’ ‘) or (s=’.’);
B[i].ch:=p;
T:=T+1;
end;
until (s=’.’);
vvod:=»;
end;
begin
T:=0;
assign(input,’input.txt’);
assign(output,’output.txt’);
reset(input);
rewrite(output);
src:=0;
prc:=0;
srh:=0;
prh:=0;{
writeln(‘Vvedite razmer massiva’);
read(N);
writeln(‘Vvedite elementi massiva’);
for i:=1 to N do
begin
read(x[i]);
y[i]:=x[i];
end;
chsort(x);
writeln;}
{
writeln(‘Kolichestvo sravnenii i prisvaivanii dlya chelnok ‘,src,’ ‘,prc);
writeln(‘Kolichestvo sravnenii i prisvaivanii dlya heapsort ‘,srh,’ ‘,prh);
writeln(‘Rezultati sortirovki: 1)chelnok for X, 2)heapsort for Y’);
for i:=1 to N do
write(x[i],’ ‘);
writeln;
for i:=1 to N do
write(y[i],’ ‘); }
{probi vivoda}
vvod(B);
hs:=T;
N:=T;
heapsort(B);
{chsort(B);}
{writeln(prh,’ ‘,srh);}
{writeln(B[1].ch^.c);}
{writeln(compare(B[1],B[2]));}
{writeln(B[1].n,’ ‘,B[2].n);}
{writeln(B[1].ch^.next^.next^.c);}
{writeln(compare(B[1],B[3]));}
{writeln(T);}
for i:=1 to T do
begin
new(p);
p:=B[i].ch;
vsgn:=true;
while (B[i].ch<>nil) do
begin
if (B[i].sgn=true) and vsgn then begin vsgn:=false; write(‘-‘) end;
write(B[i].ch^.c);
B[i].ch:=B[i].ch^.next;
end;
write(‘ ‘);
B[i].ch:=p;
end;
{end probi}
readln;
readln;
close(input);
close(output);
end.
Asked
6 years, 4 months ago
Viewed
585 times
Whenever I run SafteyDepositBox.SetNewCode
I get a runtime error 216
. Any idea whats causing this?
This is the error :
Runtime error 216 at $00401EFC
$00401EFC
$0040153D$00401596
$00406E31
program Boxy;
{$MODE OBJFPC}
{$M+}
type
SDB = class
private
State : string;
Code : string;
public
Constructor Create();
procedure SetNewCode(newcode:string);
function Valid(s:string):boolean;
end;
constructor SDB.Create();
begin
State := 'Open-NoCode';
Code := '';
end;
procedure SDB.SetNewCode(newcode:string);
begin
Code := newcode;
writeln(Code);
end;
function SDB.Valid(s:string):boolean;
var
IsValid : boolean;
begin
If (length(s) = 4) then
IsValid := true
else
IsValid := false;
Valid := IsValid;
end;
var
SafetyDepositBox : SDB;
begin
SafetyDepositBox.Create();
SafetyDepositBox.SetNewCode('r2d2');// runtime error 216 here
end.
bxc00zzy
1042 silver badges14 bronze badges
asked Feb 7, 2017 at 4:50
2
OMG you just made me remember Pascal!
This is how you call the object constructor:
SafetyDepositBox := SDB.Create();
answered Feb 7, 2017 at 5:15
MK.MK.
33.5k18 gold badges74 silver badges111 bronze badges
1
Ошибка защиты (GPF).
Описание
Эта ошибка возникает, если вы пробуете обращаться к памяти, доступ к которой закрыт вашему приложению. Операционная система останавливает ваше приложение и сообщает, что произошла ошибка защиты.
Следующие действия обычно вызывают GPF:
- Загрузка констант в сегментные регистры
- Выполнение арифметических операций на сегментных регистрах селекторов
- Использование сегментных регистров для временного хранения
- Запись в сегменты кода
- Доступ к памяти вне локального адресного пространства, данного вашему приложению
- Переименование нулевых указателей