Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2015-08

EAccessViolation

4 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Chris Moody

Hi all,

I have this procedure in my program:

procedure DisplayNode (var node:astring);
var c, i:integer;
begin
   i:=SizeOf(node);
   if (i=0) then begin
     writeln ('node was empty / non existant');
     exit;
   end;
   for c:=0 to i do
       writeln (node[c]);    // line 85
end;

astring is an Array of String

The code compiles fine, however when I run it I get:

An unhandled exception occurred at $0000000000400B3A : EAccessViolation : Access violation
$0000000000400B3A line 85 of Dentist.pas
$0000000000400C8C line 95 of Dentist.pas

Line 85 is the writeln above, line 95 is where I call this procedure from the main code.

How do I track down what is going on?

If your familiar with PHP, my purpose of this procedure is to basically emulate the following code:
<pre><?php print_r (node); ?></pre>

Thanks in advance,

Chris

Reply by email

Michael Van Canneyt
to Chris Moody
Chris Moody wrote 8 lines
Hi all, I have this procedure in my program: procedure DisplayNode (var node:astring); var c, i:integer; begin i:=SizeOf(node);

This should be length(node)

Show quoted text 6 lines
if (i=0) then begin writeln ('node was empty / non existant'); exit; end; for c:=0 to i do writeln (node[c]); // line 85

This should be

   for c:=0 to i-1 do
       writeln (node[c]);    // line 85

Michael.

Reply by email

Christo Crause
to Chris Moody: I have this procedure in my program: procedure DisplayNode (var node:astring); var c,…
"Chris Moody" wrote 27 lines
Hi all, I have this procedure in my program: procedure DisplayNode (var node:astring); var c, i:integer; begin i:=SizeOf(node); if (i=0) then begin writeln ('node was empty / non existant'); exit; end; for c:=0 to i do writeln (node[c]); // line 85 end; astring is an Array of String The code compiles fine, however when I run it I get: An unhandled exception occurred at $0000000000400B3A : EAccessViolation : Access violation $0000000000400B3A line 85 of Dentist.pas $0000000000400C8C line 95 of Dentist.pas Line 85 is the writeln above, line 95 is where I call this procedure from

the main code.

How do I track down what is going on? If your familiar with PHP, my purpose of this procedure is to basically

emulate the following code:

Show quoted text 5 lines
<pre><?php print_r (node); ?></pre> Thanks in advance, Chris

SizeOf(node) will return the size of the pointer to node. I suspect you want to loop over elements in an array so use length(node) to find out the number of elements in node.

Reply by email

Sven Barth
to Michael Van Canneyt: This should be length(node) This should be for c:=0 to i-1 do writeln (node[c]); //…
"Michael Van Canneyt" wrote 13 lines
> if (i=0) then begin > writeln ('node was empty / non existant'); > exit; > end; > for c:=0 to i do > writeln (node[c]); // line 85 This should be for c:=0 to i-1 do writeln (node[c]); // line 85

Or alternatively

for c := Low(node) to High(node) do
  Writeln(node[c]);

Or:

for n in node do // n is a var declared as String
  Writeln(n);

Note: you can't modify array elements in the for-in-statement, as the iterator variable merely contains a copy; however if the iterator variable is a pointer type (or class/interface instance) then you can modify the element's contents of course, but not the element itself (e.g. changing the pointer value).

Regards,
Sven

Reply by email