Page 1 of 1

Does a fixed string prevent accidental writing beyond allocation boundaries?

Posted: Mon Dec 18, 2023 9:59 pm
by Mistrel
If I allocate a fixed string, do I also need to perform bounds checking to ensure that assignments to that string do not exceed beyond its boundaries? I'm hesitant to say "yes", but I would like a official response. Are there any edge cases which I should be aware of?

See this example where I write beyond the end of a string but cannot read back those characters (I see garbage instead).

Code: Select all

Structure String5
  s.s{5}
EndStructure

a.String5

a\s="abcdefghij"

Debug PeekS(@a\s+5,5) ;/ Initial garbage visible
Debug a\s
Debug PeekS(@a\s+5,5) ;/ The same garbage is visible (no string characters)

Re: Does a fixed string prevent accidental writing beyond allocation boundaries?

Posted: Mon Dec 18, 2023 10:25 pm
by Olli
You should test a little bit more complex structure :

Code: Select all

Structure x
 s.s{5}[10]
EndStructure

Define x.x
x\s[0] = "NEWYORK"
x\s[1] = "SANFRANCISCO"
For i = 0 To 1
 Debug x\s[i]
Next

Re: Does a fixed string prevent accidental writing beyond allocation boundaries?

Posted: Tue Dec 19, 2023 10:05 am
by Fred
Fixed string are limited to the declared size, so if you assign a longer string, it will be truncated, but it's perfectly valid.