What is an "@Ptr" ?

BlitzMax Forums/BlitzMax Beginners Area/What is an "@Ptr" ?

AarbronBeast(Posted 2009) [#1]
Hi all.

What's the purpose of putting an AT sign (@) before Ptr? The two function calls below produce the same output. What's @ mean? For example:

Function MyFunc1(aParam Ptr)
Print aParam[0]
End Function

Function MyFunc2(aParam @Ptr)
Print aParam[0]
End Function

Local a = 100
Local ap Ptr = Varptr a

MyFunc1(ap) ' Prints 100
MyFunc2(ap) ' Also prints 100



By the way, what are the tags used in this forum to display code snippets?

Thanks!


GW(Posted 2009) [#2]
@ is shorthand for :Byte. in the same way that % is short for :int


AarbronBeast(Posted 2009) [#3]
Ah, many thanks! That is not documented in the BM docs, is it?


AarbronBeast(Posted 2009) [#4]
Are there any other type tags that I might be unaware of? So far, I know of these:

@
%
#
!
$
$w
$z


plash(Posted 2009) [#5]
You can also use @@ for Shorts.


AarbronBeast(Posted 2009) [#6]
Thanks Plash! I'll make a note of this.

Where do you guys find all this undocumented stuff? Honestly!


plash(Posted 2009) [#7]
Where do you guys find all this undocumented stuff? Honestly!
That's a secret ;)

Honestly, though, I have no idea. I've noticed the @@ in some of Nilium's code, which he probably picked up from someone else's code.
There are a bunch of undocumented, strange and interesting things you can do in Max.

For example, short-hand ternary operations (which, sadly, can only be used to set integer variables):
SuperStrict

Framework brl.standardio

Local myval:Int
Local condtrue:Int = 100, condfalse:Int = 50

Print("myval = False ? " + condtrue + " : " + condfalse + "")
myval = False And condtrue Or condfalse
Print(myval)

Print("myval = True ? " + condtrue + " : " + condfalse + "")
myval = True And condtrue Or condfalse
Print(myval)


Which Nilium also figured out, somehow.


Scienthsine(Posted 2009) [#8]
Why the hell isn't this in the docs under Help->Language->Variables? >.<


AarbronBeast(Posted 2009) [#9]
Plash, that's very interesting! Although I'd file that under clever programming rather than an undocumented language feature. :)


AarbronBeast(Posted 2009) [#10]
By the way, anyone know what a "wstring" is anyway? (Tag: $w) A wide c-string? Unicode?


Czar Flavius(Posted 2009) [#11]
That isn't something unique to Max, but it's usually considered dangerous because it depends upon the order in which the Ands and Ors are evaluated. In most languages this is left undefined, so could vary from compiler to compiler, so should be avoided. Of course, the BMax compiler is unlikely to fundamentally change any time soon or a competing compiler pop up.. but it's the principle that counts, right?? Well if you use it, just make sure you understand the evil magics you are using :P


Jasu(Posted 2009) [#12]
I think Ands and Ors are evaluated always the same way. And is always stronger than Or, so it gets evaluated first. At least this is how I remember from math class. Over ten years ago... :)
Print (False And False Or True And True) ' returns True
Print (False And False) Or (True And True) ' returns True
Print (False And (False Or True) And True) ' returns False

Cherish your boolean math. This:
R = x1*y1
If X1>Y1
   R = R + x2
EndIf

Is 50 times faster when written like this:
R = x1*y1 + x2 * (X1>Y1)

'IF' evaluation gets pretty hidden, so it's a bit more difficult to read, but the advantage is huge.


Czar Flavius(Posted 2009) [#13]
I think Ands and Ors are evaluated always the same way
Maybe you are right. I can't be bothered to ponder it any further :) But it just set off some alarm bells in my mind..

the advantage is huge.
There is an advantage, but 'huge', depends.. If that kind of thing is really the bottleneck for your code, then you can change it later, but readable code is important too..


Jasu(Posted 2009) [#14]
You're right. No reason to use it in many situations. But in cases where it can be used, it should. You can of course leave the more understandable version in comments, as I do.

I benchmarked the above code inside for loop with 10 000 000 iterations.
The normal way took 115 ms and the one line version took 5 ms. I think the difference deserves to be called huge.


Czar Flavius(Posted 2009) [#15]
If that is the bottleneck of your code, then yes it is huge and should be done :D

But I don't often have 10,000,000 iterations per game update ;)