5.20b2, pointers vs. native types.

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

5.20b2, pointers vs. native types.

Post by jassing »

I'm finding it more useful (than not) to have pointers with types -- even if those types are ignored.
For instance, if I have a *pt.b I know that the pointer is going to point to a byte -- internally, I know it is ignored; but reading code, it's nice to read what it thinks it should be. A pointer to a non-native type still points to an address in memory -- but we allow that. Just for readability, with a note "the type is actually ignored for all pointers"
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: 5.20b2, pointers vs. native types.

Post by STARGÅTE »

jassing wrote:if I have a *pt.b I know that the pointer is going to point to a byte
For this point, there are the Structurs: Byte, Ascii, Character, Long ....
the you can read it too:

Code: Select all

Structure Test
	*Pointer.Byte
EndStructure

Define Value.b = 45

Define Test.Test
Test\Pointer = @Value

Debug Test\Pointer\b
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: 5.20b2, pointers vs. native types.

Post by IdeasVacuum »

...well, what you really need to do is postfix and/or prefix your var names, common practice in most languages.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: 5.20b2, pointers vs. native types.

Post by luis »

jassing wrote:I'm finding it more useful (than not) to have pointers with types -- even if those types are ignored.
This "feature" has just been removed, so I doubt is coming back.
As already suggested, just use a meaningful prefix.
jassing wrote: A pointer to a non-native type still points to an address in memory -- but we allow that.
Because there is a reason. It permits to access memory at different offsets from the base address of the pointer specifying a structure's field.
For a native type you can just use Peek*() and if you don't want the function overhead you can use the corresponding structure (Integer for .i and so on).
"Have you tried turning it off and on again ?"
A little PureBasic review
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: 5.20b2, pointers vs. native types.

Post by jassing »

IdeasVacuum wrote:...well, what you really need to do is postfix and/or prefix your var names, common practice in most languages.
Yes yes, hungarian notation.. yes yes.... still I found it useful.
PMV
Enthusiast
Enthusiast
Posts: 727
Joined: Sat Feb 24, 2007 3:15 pm
Location: Germany

Re: 5.20b2, pointers vs. native types.

Post by PMV »

A pointer in PB is a native type like long, float, integer, ...
It is just not declared with a dot, but a star at the beginning.
If you declare a variable as a pointer and a word, it is like
declaring a variable as a integer and a word. :lol:

It just has worked because the compiler ignores the 2nd
declaration if it is declared as a pointer, but that is bad
behavior and was discussed a few times here and in
the other forums. ... at the end it is up to Fred how
it works or doesn't work. As i remember (is a long
time ago), Fred replied that it shouldn't be used ... so
it was just a matter of time till he "fixed" it. :)

PB uses pointers like integers, as long as they doesn't
have a structure. If they have, the pointer is accessible
like a structured variable. The only difference is, that
you have to deal with the blank memory for the structure
while PB manages the memory of a normal (not pointer)
structured variable itself and you just have to use
the fields.


As a side note, a month ago or less .. i have seen a
post from freak with a variable declared with a pointer
and another native type. :mrgreen:
But no liability assumed! :wink:

MFG PMV
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: 5.20b2, pointers vs. native types.

Post by skywalk »

Yeah, I had many thousands of prototypes to edit because of this.
(*mypointer) leaves me guessing months later and I hate Hungarian notation, so no prefix stuff.
I took advantage of the decorated pointer(*mypointer.d) to tell me the underlying native type via the Context help.
No harm no fouls.
Now I have to add and carry type information with the variable names, (*mypointer_d), but not (*mypointer_d.i) which is superfluous.
So, the compromise is replace the '.' with an '_' and carry on. :wink:

Code: Select all

;; FROM PB 5.0 code:
PrototypeC.l x(h.l,cp.u,*type.u,*points.i,*ti.d,*to.d,*vi.d,*vo.d)
;; TO   PB 5.1 code:
PrototypeC.l x(h.l,cp.u,*type_u,*points_i,*ti_d,*to_d,*vi_d,*vo_d)
;; Or
PrototypeC.l x(h.l,cp.u,*type.unicode,*points.integer,*ti.double,*to.double,*vi.double,*vo.double)
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: 5.20b2, pointers vs. native types.

Post by davido »

For what its worth, I use a suffix: 2b 2d 2f 2i 2q etc.

I find the .f .d etc. somewhat confusing, when used with pointers; just my humble opinion.
DE AA EB
Post Reply