Does a fixed string prevent accidental writing beyond allocation boundaries?

Just starting out? Need help? Post your questions and find answers here.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Does a fixed string prevent accidental writing beyond allocation boundaries?

Post 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)
Olli
Addict
Addict
Posts: 1203
Joined: Wed May 27, 2020 12:26 pm

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

Post 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
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

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

Post 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.
Post Reply