Passing strings efficiently (Ed Davis)
9 Mar 2003 17:36:26 -0500

          From comp.compilers

Related articles
Passing strings efficiently (2003-03-09)
Re: Passing strings efficiently (Joachim Durchholz) (2003-03-14)
Re: Passing strings efficiently (Martin Ward) (2003-03-14)
| List of all articles for this month |

From: (Ed Davis)
Newsgroups: comp.compilers
Date: 9 Mar 2003 17:36:26 -0500
Keywords: storage, performance, comment
Posted-Date: 09 Mar 2003 17:36:26 EST

I have a very simple programming language. It generates
code for a stack-based Virtual Machine. A contrived example:

----------starts here--------------------------------------
string s = "testing" -- s is a global string

procedure bad_practice() -- legal, but nasty
        s = "changed!" -- changes the value of s

procedure foo(string s2) -- s2 is passed by value
        print("foo:s:", s)
        print("foo:s2:", s2)
        print("foo:s:", s)
        print("foo:s2:", s)

foo(s) -- mainline code, calls foo, passing s by value
----------ends here----------------------------------------

Initially, I passed by-value strings by making a copy of the string,
and sending the address of the copy to the called procedure. When the
procedure returned, the copy was deleted.

However, I was thinking that I could just pass the address of a
special string structure, with some flags to indicate that it is
read-only. Then, I would only have to make a copy of the string if
foo() actually modified it. Of course, I'd have to be careful to free
the copy when foo() completes.

Well, I kind of got that working. But then I came up with this
example. It breaks my code - apparently my implementation is not

I'm having a hard time figuring out what information I need to keep in
my string structure, and how I would go about making the necessary
adjustments when another routine changes the string in a unexpected
(to me!) fashion.

Or perhaps I'm looking at this in the wrong way? I was assuming that
changing the global string would cause a copy to be made to be used
locally (by foo), but perhaps it is the global string that should
point to the new copy? But that doesn't seem right either.
[I think you're reinventing garbage collection. -John]

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.