Recent

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

Simi

  • Newbie
  • Posts: 4
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: 604
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
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: 2260
Project:Project Options :Debugging : Insertions;

Its at the top of the last window you open..
Number 1 at blue screen app creations!

howardpc

  • Hero Member
  • *****
  • Posts: 3276
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: 257
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? ^^)

Thaddy

  • Hero Member
  • *****
  • Posts: 9424
If there is no [1] you will get a sigsev. Maybe you did not understand that? It was already explained.
also related to equus asinus.

ASerge

  • Hero Member
  • *****
  • Posts: 1433
Alternatively
Not alternatively, but in addition. The code should certainly do the checking. And to facilitate debugging assert can be added.