'?' operator for pointer bounds and null check

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

'?' operator for pointer bounds and null check

Post 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)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Note: .q is not the same type as .quad.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

Are you sure?

Code: Select all

Structure Quad
  q.q
EndStructure
#NULL
Addict
Addict
Posts: 1497
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Post 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)
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.
oh... and have a nice day.
#NULL
Addict
Addict
Posts: 1497
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Post 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.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post 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.
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post 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.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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.
Post Reply