String class confusion

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

String class confusion

dazednconfused
I can't seem to make the String class work.

If I declare the class Array (for the string) as a field I get the error message:

'This' segment must be in the Heap range in String.appendChar.2 .


If I declare the class Array as static, then the output from the test shows that the printed string has overwritten the previously built s string:

new, appendChar:  new,a  

Of course it should be:
new, appendChar:  abcde
 
I'd sure appreciate some clues as to what is going on. Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: String class confusion

cadet1620
Administrator
As you determined, the Array that holds the characters must be a field so that every String can have unique data.

You should be testing/debugging your String class in the StringTest directory without trying to use any of your other OS code. Subtle bugs in your other code that may not have been found by the unit tests can make debugging far more difficult.

The most common cause of the "This segment..Heap Range" error is a pointer in memory that got overwritten, either in your object (the character Array's pointer) or in Memory's heap data structures.

If you haven't learned how to use breakpoints in the VM Translator, now's your chance.

Load the StringTest directory.
Set a "currentFunction" breakpoint with value "String.appendChar".
Run the test. It will stop at every VM command in appendChar so that you can examine what's going on.

If you haven't already done so, you might want to modify your compiler to include the source code as comments in the .vm file; it makes finding your way around in the VM code a lot easier.  (You'll need to have the VM file open in a text editor since the VM Emulator discards all comments.)
/// 67:     /** Appends the character c to the end of this String.
/// 68:      *  Returns this string as the return value. */
/// 69:     method String appendChar(char c) {
/// 70:         if (len = max) {
function String.appendChar 0
push argument 0
pop pointer 0
push this 1
push this 0
eq
if-goto L9
goto L10
label L9
/// 71:             do Sys.error(17); }
push constant 17
call Sys.error 1
pop temp 0
/// 72:         let chars[len] = c;
label L10
push this 1
push argument 1
pop temp 0
--Mark


Reply | Threaded
Open this post in threaded view
|

Re: String class confusion

dazednconfused
Thanks for the quick reply Mark. Your suggestion sent me down a path that eventually led to me seeing my mistake. I appreciate it!