Recent

Author Topic: How to assign value to record using pointer (SIGSEGV runtime error)  (Read 613 times)

dculp

  • Full Member
  • ***
  • Posts: 150
In the following code I want to assign a value to KeyCode. However, I get a runtime error "raised exception class 'External: SIGSEGV' ".
Note that the TKey record can't be changed since it's actually used as part of a much larger project.
Thanks,
Don C.

Code: Pascal  [Select][+][-]
  1. program exPointers3;
  2. // https://www.tutorialspoint.com/pascal/pascal_pointers.htm
  3.  
  4. type
  5.   PKey = ^TKey;
  6.   TKey = record // ***** CAN'T CHANGE *****
  7.     KeyCode: Smallint;
  8.     {other items}
  9.   end;
  10.  
  11. var
  12.    ZPkey: PKey;
  13.    ZTkey: ^TKey;
  14. //   ZTkey: PKey;
  15.  
  16.  
  17. var
  18.    number: integer;
  19.    iptr: ^integer;
  20.    y: ^word;
  21.  
  22. begin
  23.    iptr := @number;
  24.       {If above isn't used then the next line gives runtime error -
  25.       "raised exception class 'External: SIGSEGV'}
  26.    iptr^ := 200;
  27.    writeln('Number is: ', number);
  28.    writeln('iptr points to a value: ', iptr^);
  29.  
  30.    y := addr(iptr);
  31.    writeln(y^);
  32.    writeln;
  33.    readln;
  34.    // Above demo from https://www.tutorialspoint.com/pascal/pascal_pointers.htm
  35.    // Runs OK.
  36.  
  37.    Number:= 10;
  38.    with ZTkey^ do
  39.       KeyCode:= Number;  // runtime error - "raised exception class 'External: SIGSEGV'
  40. //   ZTkey^.KeyCode:= 10; // runtime error - "raised exception class 'External: SIGSEGV'
  41.  
  42.    writeln('ZTkey^.KeyCode = ', ZTkey^.KeyCode); // runtime error
  43.    readln;
  44. end.
  45.  

440bx

  • Hero Member
  • *****
  • Posts: 5166
Re: How to assign value to record using pointer (SIGSEGV runtime error)
« Reply #1 on: March 20, 2025, 06:42:06 pm »
ZTKey has not been assigned the address of a TKey variable therefore its value is nil (because it is a global variable) which when dereferenced produces an access violation.

Solution: assign an appropriate address to ZTKey before the "with ZTKey^ do" statement.

HTH.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

d4eva

  • New Member
  • *
  • Posts: 29
Re: How to assign value to record using pointer (SIGSEGV runtime error)
« Reply #2 on: March 20, 2025, 07:36:32 pm »
You need an actual record for ZTkey to point to, e.g.

Code: Pascal  [Select][+][-]
  1. program exPointers3;
  2. type
  3.   PKey = ^TKey;
  4.   TKey = record
  5.     KeyCode: smallint;
  6.   end;
  7.  
  8. var
  9.   ZTkey: PKey;
  10.   key: TKey;
  11.  
  12. var
  13.   number: smallint;
  14.  
  15. begin
  16.   ZTkey := @key; // now ZTkey points to key
  17.   Number := 10;
  18.   with ZTkey^ do begin
  19.     KeyCode := Number;
  20.     writeln('ZTkey^.KeyCode = ', ZTkey^.KeyCode);
  21.     ZTkey^.KeyCode := 20;
  22.     writeln('ZTkey^.KeyCode = ', ZTkey^.KeyCode);
  23.   end;
  24.   readln;
  25. end.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8365
Re: How to assign value to record using pointer (SIGSEGV runtime error)
« Reply #3 on: March 20, 2025, 08:50:09 pm »
ZTKey has not been assigned the address of a TKey variable therefore its value is nil (because it is a global variable) which when dereferenced produces an access violation.

Solution: assign an appropriate address to ZTKey before the "with ZTKey^ do" statement.

In slightly more detail, ZTKey is nil because globals are (normally) zeroed. If it were a local variable (i.e. on the stack) it would simply contain rubbisch, which might or might not be a valid address and would likely be far more difficult to faultfind.

I'd add that it's unusual and refreshing for somebody asking a question to post a complete and succint compilable example (although for completeness adding the FPC version etc. is always useful). Keep up the good work!

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018