Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

Author Topic: Exeption-class "External:SIGEGV" Trying to take the first digit of a string  (Read 773 times)

Simi

• Newbie
• Posts: 4
Exeption-class "External:SIGEGV" Trying to take the first digit of a string
« 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

MarkMLl

• Hero Member
• Posts: 1319
Re: Exeption-class "External:SIGEGV" Trying to take the first digit of a string
« Reply #1 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
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

Simi

• Newbie
• Posts: 4
Re: Exeption-class "External:SIGEGV" Trying to take the first digit of a string
« Reply #2 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.

jamie

• Hero Member
• Posts: 3640
Re: Exeption-class "External:SIGEGV" Trying to take the first digit of a string
« Reply #3 on: December 10, 2019, 10:50:38 pm »
Project:Project Options :Debugging : Insertions;

Its at the top of the last window you open..
The only true wisdom is knowing you know nothing

howardpc

• Hero Member
• Posts: 3545
Re: Exeption-class "External:SIGEGV" Trying to take the first digit of a string
« Reply #4 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.

egsuh

• Sr. Member
• Posts: 465
Re: Exeption-class "External:SIGEGV" Trying to take the first digit of a string
« Reply #5 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? ^^)

• Hero Member
• Posts: 10516
Re: Exeption-class "External:SIGEGV" Trying to take the first digit of a string
« Reply #6 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.

ASerge

• Hero Member
• Posts: 1680
Re: Exeption-class "External:SIGEGV" Trying to take the first digit of a string
« Reply #7 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.