Menu

Temporary strings

Some people have problems how the temporary strings work in 4tH. Well, I must have done some things right, since it seems to be very transparent to most people on average, but in some extreme circumstances it may pose a problem.

When you issue S" or get some string defined by ," with @C the temporary string mechanism of 4tH kicks in. It is a circular buffer of several hundreds of bytes which resides in PAD. Contrary to "vanilla" Forth, it is "dangerous" to use PAD if you don't know what you're doing - although the worst that may happen is that you overwrite your string.

Most static strings live in a segment that is not accessible by 4tH at all. PAD takes care of that, though. So if you refer to a string in that static segment it secretly copies it to PAD and gives you the address in PAD. Consequently, you may think it was there all the time. Wrong!

PAD is divided into two areas: one is the circular buffer, the other is the area where your numerical strings are composed, e.g. when you use .. If you start 4tH fresh, a S" copies it to PAD. The next S" does the same thing, but takes care not to overwrite the string of the previous S" unless the current S" would spill into the numerical buffer. The numerical buffer can theoretically spill into the temporary string area, but the chances are rather remote - unless you actually set out to do so or do something really crazy.

But, if you try to copy a really large S" string, 4tH will not allow you to do that. It will copy the string up to the max and terminate it - that's what it will return then. Consistent, but you have a truncated string.

If you copy several large S" strings into PAD or loop through a table with lots of ," you may encounter some clobbering as well. The trick is to give the string that is clobbered it's own place in a string variable. It's as easy as that. Note that TIB and PAD don't share any space and cannot clobber each other, so if you combine those you're OK. E.g. PARSE and friends don't change anything - they just return a substring in TIB (yours or mine).

So, now you know how this 4tH subsystem works under the hood. Note I tend to forget all about it myself most of the time - and so should you. Even if you do something weird, 4tH will always try to make sense of it and return the most sane answer it can provide - given the circumstances. But it may surprise you if you're off guard.