Adding in the ability for String pointers

BlitzMax Forums/BlitzMax Programming/Adding in the ability for String pointers

Duckstab[o](Posted 2011) [#1]
Type StringEx
	Field S:String
	
	Function Create:StringEx(_S:String)
		Local i:StringEx = New StringEx
		i.s = _S:String
		Return i
	End Function
End Type


Im using renderSurfaces in my games gui and wish to keep all type loop and variable checking to a minamum when redrawing the rendersurface as they are Fps eaters

I got the float and int pointers working but strings got me stumped

So instead of have the usual name$

Type Myplayer
 Field Name:StringEx
End Type

'So i can do 

Type Gui_Popup
 Field Var:StringEx
end Type

Local Me:MyPlayer  = new MyPlayer
Local Temp:StringEx = new StringEX
Local Gui:Gui_Popup new Gui_Popup

Me.Name = Temp
Gui.Var = Temp


Me.Name.s = "Bobby" 
Print Me.Name.s
Print Gui.Var.s
Gui.Var.s = "Billy"
Print Me.Name.s
Print Gui.Var.s



Would this be a usafe or expensive approach

Last edited 2011


AdamRedwoods(Posted 2011) [#2]
I think Strings are pointers.

a$ = "whatever"
print a[0]

...or something.

You could also get the address with varptr.
Not sure if that's what you are after.


HrdNutz(Posted 2011) [#3]
Strings are immutable objects in blitzmax, meaning they are pointers to begin with and cannot be changed. Any operation on strings will result in new string objects created, so you cannot have multiple variables pointing to the same string be affected by modifying that string.

' this creaes a variable and points it to object "immutable"
Local s1:String = "immutable"

' this creates a variable and points to the same object as above
Local s2:String = s1

' this creates a NEW string object and re-points s1 to new location
s1 = "NEW string"

' s2 variable is still pointing to the original "immutable" object,
' and it makes string assignment look like the value is being copied,
' but really the original object is existing through the s2 pointer
Print s2


Look up immutable objects if you are unfamiliar.

Cheers,
Dima


Czar Flavius(Posted 2011) [#4]
I don't understand what you are trying to do with your gui, but the inital code box you provide contains the solution I would use.


Duckstab[o](Posted 2011) [#5]
@Czar
Yes im using the StringEx method at the moment and its proving very handy

my only concern is overheads

a normal character type has 10 fields 5 int 5 strings

so in memory you got a good idea how big it is

using stringex means the character now has 5 int and 5 typepointers(ie a types)

so effectively it is 6 types 5 ints and 5strings

so in memory/performance is this gonna make a big impact when im using StringEx allowing i will be using thousands

Last edited 2011


Czar Flavius(Posted 2011) [#6]
I wouldn't worry about the memory, it's 12 extra bytes per string which is nothing.

As for speed, strings aren't very fast to begin with so I would say it wouldn't be noticable, but it depends what you are using them for, which I don't understand.