FlexBASIC garbage collection and null-terminated strings
JRoark
Posts: 1,215
in Propeller 2
I'm running FlexBASIC, current version, on a P2-EVAL Rev B under Win10. I'm trying to chase down what appears to be memory leak.
Assume that I create a string:
Now assume that after creating this string, I decide to wedge a null character (Chr$(0)) between "This" and " is". When the string is ultimately released, does the garbage collection routine understand that it needs to reclaim the whole 15 character string memory space, or does it hit that null at offset 5 and stop there?
Bueller? Anyone?
Assume that I create a string:
dim myStr$ as string myStr$ = "This is a test"
Now assume that after creating this string, I decide to wedge a null character (Chr$(0)) between "This" and " is". When the string is ultimately released, does the garbage collection routine understand that it needs to reclaim the whole 15 character string memory space, or does it hit that null at offset 5 and stop there?
Bueller? Anyone?
Comments
True, that. But when you're trying to make things really scream, and you are faced with limited processor resources, sometimes you need to resort to more unconventional bits of code. In fact, ERSmith works exactly this sort of magical mischief using pointers, ubytes and forced nulls in his "strings.bas" lib included with FlexBASIC. And for good reason: A comparable library sans pointers, forced nulls, etc, runs about 5x slower.
I have a bad feeling that the GC just looks for that null, finds it, and calls it good. That is one of the problems of using the C-style string convention instead of using a string table that contains not only the address of the string, but a length, and potentially an indicator as to UTF-8, UniCode, etc. The C-style is somewhat faster, but it also has serious limitations (like the inability to have a string contain a null as a valid character unless you want to jump through some hoops).
It's Bueller's day off so he can't give you an answer, and neither can I. Why not write a little test program to find out.
Snicker. Ok. I earned that one.
@ersmith Good to see you back, if only briefly. Thanks for the answer!