Re: stack overflow avoidance?
Re: stack overflow avoidance?
- Subject: Re: stack overflow avoidance?
- From: Nigel Garvey <email@hidden>
- Date: Sun, 12 Nov 2000 15:12:27 +0000
Jeff Ganyard wrote on Sat, 11 Nov 2000 20:57:47 -0800:
>
I'm have a looping routine that runs through text changing things...
>
it's pretty simplistic.
>
>
get offset of start string
>
get offset of end string
>
change text in between
>
concatenate "chars 1 thru first offset" and "changed text" and "chars
>
second offset to end of string"
>
>
It seems to work fine most of the time, if the_string is not "too
>
long" it works fine. However in some cases the_string is "too long"
>
and I get a stack overflow error. How do I avoid that error?
As you've probably guessed, it's the concatenation line that causes the
problem. It makes copies of the two kept parts of the string, then
constructs a new string from these. So you end up with:
The original string
Part 1, the changed text, and part 2
An intermediate new string
- ie. about three times as much string in memory as you started with.
This is probably the best way for very short strings or for single text
replacements. But when you're replacing several parts of a long string,
you have to remember that each subsequent operation also creates:
Another part 1, another changed text, another part 2
Another intermediate new string
- adding another two string lengths to the memory. A similar situation
exists when using text item delimiters, with the additional overhead of
the list variables - except that this time, all instances of a particular
change are handled at once, which reduces the number of intermediate new
strings created.
The most memory-efficient method (though possibly not as fast as text
item delimiters) is probably:
Create an empty list
Locate the first change point in the string
Set the end of the list to the text up to that point
Set the end of the list to the replacement text
Make the rest of the string the new search string
Repeat lines 2 to 4 until done
Set the end of the list to whatever remains of the search string
Coerce the list to string to concatenate all the bits
Saving the concatenation to the end does away with intermediate new
strings altogether and produces shorter extracts. (Text already extracted
isn't included in text that's extracted later on.)
In practice, you'll want to choose between the various methods - or mix
them - according to the length of the string and the number and type of
different changes you want to do.
NG