Page 1 of 1

'?' operator for pointer bounds and null check

Posted: Wed Jul 01, 2009 5:15 am
by Mistrel
Consider this example:

Code: Select all

Procedure This(*That.Quad)
  ;/ We can check for a null pointer but not for an out of bounds exception
  If Not *That
    ProcedureReturn
  EndIf
  Debug *That\q ;/ Null pointer causes runtime error here
EndProcedure

Define N.q=9223372036854775807
Define Q.b

This(@N) ;/ Ok!
This(@Q) ;/ Many crash/may not crash
This(0)  ;/ Runtime error only if procedure attempts to access it
There is no guarantee that an acceptable type is being passed using a simple pointer. This functionality can sometimes be desired. However it would be nice to be able to do bounds and null checking automatically to prevent errors and also to simplify code.

It is currently possible to check for a null pointer but this check is so common that it would be a benefit to have the functionality built in.

My suggestion is:

Code: Select all

;/ Automatic bounds and null check using '?*' in place of '*' for parameter lists
Procedure This(?*That.Quad)
  Debug *That\q
EndProcedure

Define N.q=9223372036854775807
Define Q.b

This(@N) ;/ Ok!
This(@Q) ;/ Compiler error here!
This(0)  ;/ Runtime error here!
This feature would require labels to be considered out of scope for the function when its prototype specifies a parameter with the same name.

These ideas are taken from Cyclone's concept of the safe pointers:

http://en.wikipedia.org/wiki/Cyclone_(p ... _language)

Posted: Wed Jul 01, 2009 8:37 am
by Trond
Note: .q is not the same type as .quad.

Posted: Wed Jul 01, 2009 8:56 am
by Mistrel
Are you sure?

Code: Select all

Structure Quad
  q.q
EndStructure

Posted: Wed Jul 01, 2009 10:41 am
by #NULL
maybe trond is saying that it would be more difficult than this to decide if a passed pointer should be [in]valid:

Code: Select all

Procedure This(*That.Quad)
  Debug *That\q
EndProcedure




Structure my_quad
  q.q
EndStructure

Structure my_quad_mem
  
  a.l
  b.b
  c.b
  
  ; or maybe instead:
  ; arr.b[3]
  
EndStructure

q1.q           = 11
q2.Quad\q      = 22
q3.my_quad\q   = 33
PokeQ(@q4.my_quad_mem,44)

This(@q1)
This(@q2)
This(@q3)
This(@q4)
or the other possibility would be to allow the exact type of the definition only (.Quad only, but not .q or anything)

Posted: Wed Jul 01, 2009 11:01 am
by Kaeru Gaman
Mistrel wrote:Are you sure?

Code: Select all

Structure Quad
  q.q
EndStructure
Quad is a Structure, q is a Type.
you can structure a pointer using Quad, to be able to access the .q type behind it,
but you cannot type a pointer, it would just make the pointer 64bit long, but not work the way you want it.

Posted: Wed Jul 01, 2009 11:12 am
by #NULL
you would not type the pointer for runtime checks. only the compiler would check if the variable/value is of the desired type.

Posted: Wed Jul 01, 2009 8:26 pm
by Trond
Mistrel wrote:Are you sure?

Code: Select all

Structure Quad
  q.q
EndStructure
Of course I'm sure.

Quad is a structure that contains one member of type .q. They are not of the same type. One is a structure and the other is an intrinsic type. It's like comparing apples and orange paint. So in this case, the feature would be useless, as you're passing the address of a variable of type .q while the procedure expects the address of a variable of type .quad. So you will get a compile time type error.

Posted: Wed Jul 01, 2009 8:53 pm
by Mistrel
I think you're misunderstanding my request. I specifically said "bounds check" and not "type check". Both .Quad and .q are the same size. Requiring strict type checking here wouldn't work.

PureBasic doesn't provide facilities like C does for casting as part of an expression and pointers aren't as powerful. Because the pointer types are not as flexible it would have to be bounds checking instead of type checking. Everything in PureBasic is a void* pointer.

Posted: Wed Jul 01, 2009 10:22 pm
by Fred
It's not a void* pointer as you can associate a structure to it. Now, there is no type check when assigning a value to a pointer, which is not a big limitation IMHO.

Posted: Thu Jul 02, 2009 9:16 am
by Trond
Mistrel wrote:I think you're misunderstanding my request. I specifically said "bounds check" and not "type check". Both .Quad and .q are the same size. Requiring strict type checking here wouldn't work.
And what would be the purpose of such a bound check that doesn't check the type? It would be useless, since the parameter could expect a structure with a string variable (or any pointer), but get one of equal size, where the equivalent structure field was not a pointer (or a pointer of the wrong type).
Then you would end up with an invalid memory access even thought you thought you were safe due to the bounds check and be more confused than ever.