Lazarus

Programming => General => Topic started by: Simi on December 10, 2019, 08:12:36 pm

Title: Exeption-class "External:SIGEGV" Trying to take the first digit of a string
Post by: Simi on December 10, 2019, 08:12:36 pm
Hi, thanks for clicking on this post.
I am programming a calculator. I have a array of the type string which stores my numbers (like everytime i click on a operator the next digit of the array is getting accessed. Everything i do with the calculator works fine, if I delete the following line of code:
  if (stringname[1] in ['0'..'9'] then
Stringname is a variable of string in which I saved the specific number. I have also tried variations like writing all numbers from 0 to 10, or using the arrayname directly. I really don't know how to fix it. If you have any idea, please tell me :)
Title: Re: Exeption-class "External:SIGEGV" Trying to take the first digit of a string
Post by: MarkMLl on December 10, 2019, 09:02:13 pm
Always check that a string is non-empty before indexing into it directly.

Code: Pascal  [Select][+][-]
  1. if (stringname <> '') and (stringname[1] in ['0'..'9']) then
  2. ...
  3.  

Alternatively

Code: Pascal  [Select][+][-]
  1. Assert(stringname <> '', 'Fatal error: string unexpectedly empty");
  2. if stringname[1] in ['0'..'9'] then
  3. ...
  4.  

making sure that assertions are enabled as a compiler option.

MarkMLl
Title: Re: Exeption-class "External:SIGEGV" Trying to take the first digit of a string
Post by: Simi on December 10, 2019, 09:30:08 pm
Always check that a string is non-empty before indexing into it directly.

Code: Pascal  [Select][+][-]
  1. if (stringname <> '') and (stringname[1] in ['0'..'9']) then
  2. ...
  3.  

Alternatively

Code: Pascal  [Select][+][-]
  1. Assert(stringname <> '', 'Fatal error: string unexpectedly empty");
  2. if stringname[1] in ['0'..'9'] then
  3. ...
  4.  

making sure that assertions are enabled as a compiler option.

MarkMLl

thanks for your reply :) Checking if the sting is empty hasn't changed anything about the error, sadly. Where can I check if the assertions are enabled? Didn't find it yet, because i have the program in another language.
Title: Re: Exeption-class "External:SIGEGV" Trying to take the first digit of a string
Post by: jamie on December 10, 2019, 10:50:38 pm
Project:Project Options :Debugging : Insertions;

Its at the top of the last window you open..
Title: Re: Exeption-class "External:SIGEGV" Trying to take the first digit of a string
Post by: howardpc on December 10, 2019, 11:16:48 pm
To clarify jamie's tip:
Project Options

Compiler Options (node)
Select the Debugging subnode.
In the "Checks and assertion" groupbox on the Debugging page you have to check the "Include assertion code (-Sa)" checkbox.
Title: Re: Exeption-class "External:SIGEGV" Trying to take the first digit of a string
Post by: egsuh on December 11, 2019, 04:25:17 am
Look around some other parts where memory may not be allocated to other variables than platzhalter. And check declaration part of platzhalter. It might be re-declated as different type than string, between the classes, blocks, etc. I had similar experiences several times, and sometimes the source of error was not at the exact point the error was raised. Based on my experiences, SIGEGV is trying to access wrong memory place (platz in German? ^^)
Title: Re: Exeption-class "External:SIGEGV" Trying to take the first digit of a string
Post by: Thaddy on December 11, 2019, 07:09:52 am
If there is no [1] you will get a sigsev. Maybe you did not understand that? It was already explained.
Title: Re: Exeption-class "External:SIGEGV" Trying to take the first digit of a string
Post by: ASerge on December 11, 2019, 09:23:51 pm
Alternatively
Not alternatively, but in addition. The code should certainly do the checking. And to facilitate debugging assert can be added.
TinyPortal © 2005-2018