*Pointer.<Type> with standard types

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
PMV
Enthusiast
Enthusiast
Posts: 727
Joined: Sat Feb 24, 2007 3:15 pm
Location: Germany

[Implemented] *Pointer.<Type> with standard types

Post by PMV »

Danilo wrote: With *pointer\something like in C/C++ it would work with all structures:

Code: Select all

*pointer.POINT = AllocateMemory(100*SizeOf(POINT))

For i = 0 To 99
    *pointer[i]\x =  i
    *pointer[i]\y = -i
Next
Sometimes you get a buffer or return a buffer to sequential arrays of a type.
With pointers you can easily access this.

Now you have to save the original pointer if you need it later, like this:

Code: Select all

*pointer.POINT = AllocateMemory(100*SizeOf(POINT))

*savedPointer = *pointer
For i = 0 To 99
    *pointer\x =  i
    *pointer\y = -i
    *pointer+SizeOf(POINT)
Next
*pointer = *savedPointer

Code: Select all

Structure pArray_Point
  p.POINT[0]
EndStructure


*pointer.pArray_Point = AllocateMemory(100*SizeOf(POINT))

For i = 0 To 99
  *pointer\p[i]\x =  i
  *pointer\p[i]\y = -i
Next
  
For i = 0 To 99
  Debug *pointer\p[i]\x
  Debug *pointer\p[i]\y
Next
Of course it would be better without this "dummy"-structure, but i think
you should make a second feature request as that isn't related to this one :)
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: *Pointer.<Type> with standard types

Post by Demivec »

PMV wrote:... but i think
you should make a second feature request as that isn't related to this one :)
I agree, the suggestion for pointers and array access belongs in a new thread. I have one more comment on it though. It seems like it would be limited to just the highest level of the structure as a whole. It would definitely cause confusion if it was usable when dereferencing any nested sub-structures. If it was desirable to also access sub-structures this way (i.e. *ptr[5]\b[3]\etc[13]) then perhaps an additional bracket type could be used to differentiate . This would make the structure definitions of 'Structure MISC: i.i: EndStructure: Structure TEST: b.b[9]: *etc.MISC: EndStructure' and the pointer '*ptr.TEST', accessible with '*ptr{5}\b[3]\etc{13}\i' instead of merely being limited to '*ptr[5]\b[3]\etc\i'. I may be over-thinking things though. :wink:



Regarding the 'first' topic of this thread, I do not think it is a good idea to implement pointers of 'standard types' by replacing the types with structures. It is better to prevent using a standard type with a pointer declaration so that this wayward practice (of using standard types with pointers) can be done away with. That would work better to eliminate the confusion.


@Edit: Added a more confusing example to 'side-thread'.
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Re: *Pointer.<Type> with standard types

Post by nco2k »

imho they should disable *pointer.l etc since its useless. so if we want to use something like that, we are forced to write *pointer.long, which is imho the better way. an automatic .l -> .long transition would be kinda confusing.

c ya,
nco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: *Pointer.<Type> with standard types

Post by Mistrel »

I think a simpler alternative would be to support passing variables to procedures by reference. This would prevent the addition of a possible confusing syntax where a .<type> has different behavior depending on a * prefix.. but only for basic types.. etc.
Post Reply