Trond wrote:When assigning a new string, the string manager frees the previous string. Freeing the empty string is a bad idea if everyone points to it.
Not initialized string has a pointer of zero! There is nothing to be freed. That is the problem.
Code: Select all
Define NullString.s
Debug @NullString ;pointer is null!
Debug Chr(34) + NullString + Chr(34) ; but string-operations are the same as empty strings
Define EmptyString.s=""
Debug @EmtyString ;pointer is not null
Debug Chr(34) + EmptyString + Chr(34) ; normal string-operation as expected
I want to elemniate the NullString. I doesn't want to change the handling
of empty strings. I want, that empty strings are always used as empty
strings. Regardless it is not initialized by the programmer. That is the same
behavior like any other datatyp of purebasic. It is
never needed to initialize
a variable because PB initializes any variable with zero, if the programmer
doesn't do it. Strings are not needed to be initialized, too. Like in the code
above. Null-strings are handled like empty-strings. But only As long as you
doesn't use the pointer from it, because it is really null.
null-pointers -> IMA -> the worst case for any programmer
That is not an easygoing problem, because IMAs can have many reasons and
this reason is not documented. And i doesn't want it to be documented,
because where should it be written? The clean-way is to not let it happen.
And i hope i have (translated) a good and easy solution for fred/ freak to
implement it when they can.
Code: Select all
Define Empty1.s, Empty2.s, Empty3.s
Empty1 = "Hello" ; String manager inadvertently frees the constant string
Empty3 = Empty2 ; INVALID MEMORY ACCESS
There will be an invalid memory access on the last line, first when trying to read from Empty2 (which was freed when assigning to Empty1), then when trying to free the old string pointed to by Empty3 (which is also the same string that was freed when assigning to Empty1).
Nope ... there is no error because PB is already handling not initialized
Strings as a special case. Otherwise you would get an IMA on the that line,
because you assign a null-string (not empty) to another null-string.