String class has how many Fields?

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

String class has how many Fields?

virote328
Hi

The VM version of the string class always allocates 3.  But I can only think of two things that the string would need as a field.  The max length and the array that holds all of the letters.  Is there a third? cause if there is, I don't know what it would be

Thanks

-Virote
Reply | Threaded
Open this post in threaded view
|

Re: String class has how many Fields?

cadet1620
Administrator
Read 12.1.4 carefully.  It tells you what the third field is.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: String class has how many Fields?

virote328
Thanks, so it was the "this"
Reply | Threaded
Open this post in threaded view
|

Re: String class has how many Fields?

cadet1620
Administrator
virote328 wrote
Thanks, so it was the "this"
It's not "this". It's something critical related to the string data stored in the Array.

If you don't see it, you'll figure it out when you start writing the String methods.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: String class has how many Fields?

virote328
Alright, I believe I have finished the String class.  It passes the "StringTest" without a problem.

The Given String.vm still allocates 3 fields.  My version only does 2.  I'm still puzzled as to what that third field is ^_^


ps( Cadet, you were right, it wasn't the "this"  it would make no sense)

pss( My brain has been Jacked and Hacked way too much to think straight)
Reply | Threaded
Open this post in threaded view
|

Re: String class has how many Fields?

cadet1620
Administrator
virote328 wrote
Alright, I believe I have finished the String class.  It passes the "StringTest" without a problem.

The Given String.vm still allocates 3 fields.  My version only does 2.  I'm still puzzled as to what that third field is ^_^
This is related to the Arrays don't know their own length? discussion.

One of the problems with the OS tests is that they can't test for error handling. For instance, if your String code doesn't catch attempts to append beyond the end of a string's allocated length, the test doesn't catch that.

Try adding this final test between the disposes in Main.Jack in StringTest:
    do i.dispose();
    let s = "123456";
    do Output.printString("Should get error 17: ");
    let s = s.appendChar(55);
    do s.dispose();
[The three fields my String class has are Array pointer, current length and allocated length.]

--Mark

Reply | Threaded
Open this post in threaded view
|

Re: String class has how many Fields?

virote328
ah!  I see.  although when my string is appended I just replace it with a one bigger array.  so in your example it would print "1234567" without a problem.

unorthodox much?

Derrick Ho

On Mar 24, 2013, at 6:54 AM, "cadet1620 [via Nand2Tetris Questions and Answers Forum]" <[hidden email]> wrote:

virote328 wrote
Alright, I believe I have finished the String class.  It passes the "StringTest" without a problem.

The Given String.vm still allocates 3 fields.  My version only does 2.  I'm still puzzled as to what that third field is ^_^
This is related to the Arrays don't know their own length? discussion.

One of the problems with the OS tests is that they can't test for error handling. For instance, if your String code doesn't catch attempts to append beyond the end of a string's allocated length, the test doesn't catch that.

Try adding this final test between the disposes in Main.Jack in StringTest:
    do i.dispose();
    let s = "123456";
    do Output.printString("Should get error 17: ");
    let s = s.appendChar(55);
    do s.dispose();
[The three fields my String class has are Array pointer, current length and allocated length.]

--Mark




If you reply to this email, your message will be added to the discussion below:
http://nand2tetris-questions-and-answers-forum.32033.n3.nabble.com/String-class-has-how-many-Fields-tp4026512p4026534.html
To unsubscribe from String class has how many Fields?, click here.
NAML
Reply | Threaded
Open this post in threaded view
|

Re: String class has how many Fields?

cadet1620
Administrator
virote328 wrote
ah!  I see.  although when my string is appended I just replace it with a one bigger array.  so in your example it would print "1234567" without a problem.

unorthodox much?
Mostly just an efficiency problem, especially given the way the Jack compiler handles string constants by calling String.append() for each character in the string.

--Mark