Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the lifetime of S" ..." in Forth?

I've been trying to read up on this, but I can't find any mention of it.

According to the standard, a string created with S" can not be modified, and from a simple experiment in Gforth it's obvious that space for the string does not come from the dictionary or pad areas:

hex 
here . 7F48AB3B8758  ok
pad . 7F48AB3B8808  ok
s" test" .s <2> 77FDD0 4  ok

How long can I expect that address to be valid?

In other words, if I store this address (and count) in a variable, can I refer back to it later in the program, or do I need to move it to a separate location in the dictionary or heap? And if I don't store the address, will I leak memory?

like image 744
harald Avatar asked Sep 30 '12 07:09

harald


1 Answers

When compiled into a definition the string's lifetime is that of the definition. s" is normally used only at compile-time.

Not all Forths even allow interpretation-time use of s" and indeed ANS says, "Interpretation semantics for this word are undefined." The behavior will be specific to your particular Forth at the very least.

You appear to be using Gforth which happens to have a reserved space for at least one interpretation-time string. The Gforth manual says, "... the string exists only until the next call of s"". It goes on to say, "Some Forth systems keep more than one of these strings, but usually they still have a limited lifetime." (Section 3.24 Characters and Strings).

I hope that helps!

like image 52
AshleyF Avatar answered Oct 06 '22 01:10

AshleyF