Passing text/strings as a byte ptr?

BlitzMax Forums/BlitzMax Beginners Area/Passing text/strings as a byte ptr?

[edit] Actually, you can ignore this. I've decided to bite the bullet and just go in and modify the String source to suit my needs.


Okay, so I'm working on implementing my own string type and I've got a question regarding the underlying way BMax is going to handle something.

Here's the beginning of my type
Type TString

	Field m_data:Byte Ptr
	Field m_length:Int
	
	Function Constructor:TString( data:Byte Ptr, length:Int )
		
		Local l_NewTString:TString
		l_NewTString = New TString
		l_NewTString.m_data = data
		l_NewTString.m_length = length
		
		Return l_NewTString	
	EndFunction

	Method ReturnString:String( )
		Return string.frombytes( m_data, m_length )
	EndMethod
	
EndType


Now, if I do this
Global test$ = "This is only a test"
Global str:TString
str = TString.Constructor( test , test.length )
Print str.ReturnString()


The code does, for some reason, work. The line

Print str.ReturnString()

Does in fact return the expected text. However, modifying the m_data field has no effect whatsoever on the original string "test$'. This leads me to believe that when a string is passed into a function expecting a byte ptr (as Constructor() does), it converts itself to a normal C-string. That's fine.

What I'm curious about tho, is - if I simply pass an undefined and undeclared chunk of text into the function - how will that be handled. What will happen if I do this
str = TString.Constructor( "random text", 11 )


Calling ReturnString() works as expected. The code works. I'm just worried that when I pass in a character array, BMax is actually creating a String object out of it, and then calling .ToCString to return a byte ptr to the function.

I'm trying to avoid the extra copying and overhead of the ToCString() call, so if what I suspect above is actually happening, how might I pass some text into the function without the overhead?

Oh, and does anyone know why I can pass a string in to a function expecting a byte pointer, and it works - yet Bmax does not automatically convert a String to a byte pointer anywhere else...i.e.
str:string
bptr:byte ptr
bptr = string


returns the error "Unable to convert string to byte pointer."

Aren't Strings objects? If they are, it's my soapbox subject again, the old AnyObjectType->Byte Ptr implicit conversion in functions, even in SuperStrict. I still *really* think it's wrong to allow implicit type conversions like this in SuperStrict. Even more so when you apparently can't even do it explicitly. I thought the whole point of SuperStrict was to force the programmer to be explicit.

The correct version of above code:
str:string
bptr:byte ptr
bptr = string.toCString()  'or .toWString() if unicode


Reason is, as Gabriel mentions, that strings are objects and not array of char as in C

I'm aware that they're objects -but I feel like Gabriel

I still *really* think it's wrong to allow implicit type conversions like this in SuperStrict. Even more so when you apparently can't even do it explicitly.



What happens to the memory allocated by the implicit conversion after the function completes? What happens if that function got called 1000's of times?

I see, you start to see the point why Byte Ptr usage should be restricted to extern blocks instead of messing the garbage collector managed "world" with unmanaged data.