Page 1 of 2

*Pointer.<Type> with standard types

Posted: Sun Nov 13, 2011 4:51 am
by Danilo
In my opinion:

Code: Select all

*pointer1.i ; should be same as *pointer1.Integer
*pointer2.l ; should be same as *pointer2.Long
*pointer3.d ; should be same as *pointer3.Double
etc.
It just would be nice to have the *pointer working with standard types.
The compiler could map the standard types to the corresponding structures
for *pointers automatically.

Re: *Pointer.<Type> with standard types

Posted: Sun Nov 13, 2011 4:58 am
by Shield
Seems like a nice idea, +1. :)

Re: *Pointer.<Type> with standard types

Posted: Sun Nov 13, 2011 7:49 am
by IceSoft

Re: *Pointer.<Type> with standard types

Posted: Sun Nov 13, 2011 8:50 am
by Danilo
No, i am not asking to return structures or pointers to structures.

This works now:

Code: Select all

dbl.d = 23.456
*pointer.Double = @dbl
Debug *pointer\d
and it could be

Code: Select all

dbl.d = 23.456
*pointer.d = @dbl
Debug *pointer\d
if standard types for pointers would work like the corresponding structures.

Re: *Pointer.<Type> with standard types

Posted: Sun Nov 13, 2011 1:26 pm
by PMV
Wouldn't this make some people confuse because there would
be two different behaviors of the same statement?

Code: Select all

*Pointer.BYTE ;will create a pointer to a byte-value
*Pointer.b ;would create a pointer to a byte-value
Value.BYTE ;will create "pointer" and a byte value, the "pointer" pointers to the byte value
Value.b ;will create a byte-value
... but do i really care about other confused users? :lol:
This sounds not that bad. And as long as i think about it, it
gets really cool :mrgreen: ... but at the end, i prefer still
a simple syntax-check to truly disallow standard types with
pointers. I don't think i would use the standard types here.

MFG PMV

Re: *Pointer.<Type> with standard types

Posted: Sun Nov 13, 2011 1:39 pm
by luis
PMV wrote:Wouldn't this make some people confuse because there would
be two different behaviors of the same statement?
I don't understand what you are saying. As I understand Danilo request:

*Pointer.BYTE would be equal to *Pointer.b ; both will create a pointer to a signed byte-value
Value.BYTE ; will continue to create a BYTE structure
Value.b ;will continue to create a signed byte-value

The only change would be that *var.base_type_struct will became equivalent to *var.base_type.

If I understood correctly. I'm am not eager for such change, because as it is feels more coherent with the use of pointers to GENERIC user defined structures. But I would simply avoid to use it.

What I would like a lot more it would be the ability to add a integer value to a pointer in a expression and assign a new value to the resulting address of the pointer expression. As the things works now, you have to modify the value of pointer var each time. It's a pain because this alter the pointer, so if you don't want that you have to save the original pointer to restore it later, or use another pointer var of the same type, copy the value of the original pointer to it, and then alter the copy.

Re: *Pointer.<Type> with standard types

Posted: Sun Nov 13, 2011 1:50 pm
by Danilo
luis wrote:If I understood correctly.
Yes. *pointer.b/.i/.d are useless atm, they do nothing.

Happened quite often to me that i used standard types like *pointer.i
and found the mistake after i wanted to access the data by writing *pointer\
but no popup window came to show me the i. :D
luis wrote:What I would like a lot more it would be the ability to add a integer value to a pointer in a expression and assign a new value to the resulting pointer value. As the things works now, you have to modify the value of pointer var each time. It's a pain because this alter the pointer, so if you don't want that you have to save the original pointer to restore it later, or use another pointer var of the same type, copy the value of the original pointer in it, and the alter the copy.
Like this?

Code: Select all

*pointer.Byte = AllocateMemory(100)

For i = 0 To 99
    (*pointer+i)\b = i
Next
I would like it same as in C/C++:

Code: Select all

*pointer.Byte = AllocateMemory(100)

For i = 0 To 99
    *pointer[i]\b = i
Next
Every pointer can also be accessed as an array of pointer type.

+1 :)

Re: *Pointer.<Type> with standard types

Posted: Sun Nov 13, 2011 3:24 pm
by Shield
Also +1 on this!
That would make PB's pointer a bit more usable...currently it's just a mess having to fool around with them
when more complex pointer arithmetic is involved. :)

Re: *Pointer.<Type> with standard types

Posted: Sun Nov 13, 2011 4:46 pm
by PMV
Danilo wrote:I would like it same as in C/C++:

Code: Select all

*pointer.Byte = AllocateMemory(100)

For i = 0 To 99
    *pointer[i]\b = i
Next
Every pointer can also be accessed as an array of pointer type.

+1 :)
Not that readable but possible:

Code: Select all

Structure pArray_Byte
  p.BYTE[0]
EndStructure

*pointer.pArray_Byte = AllocateMemory(100)

For i = 0 To 99
  *pointer\p[i]\b = i
Next
  
For i = 0 To 99
  Debug Str(*pointer\p[i]) + " -> " + Str(*pointer\p[i]\b)
Next
:D
or do i have get something wrong? :oops:

MFG PMV

Re: *Pointer.<Type> with standard types

Posted: Sun Nov 13, 2011 4:50 pm
by DarkDragon
Danilo wrote:
luis wrote:If I understood correctly.
Yes. *pointer.b/.i/.d are useless atm, they do nothing.

Happened quite often to me that i used standard types like *pointer.i
and found the mistake after i wanted to access the data by writing *pointer\
but no popup window came to show me the i. :D
Maybe it would be better to disallow standard types for pointers. That wouldn't confuse the people so much, would it?
But yes, it's a nice request as the current situation isn't good.

Re: *Pointer.<Type> with standard types

Posted: Sun Nov 13, 2011 5:14 pm
by Danilo
PMV wrote:Not that readable but possible:

Code: Select all

Structure pArray_Byte
  p.BYTE[0]
EndStructure

*pointer.pArray_Byte = AllocateMemory(100)

For i = 0 To 99
  *pointer\p[i]\b = i
Next
  
For i = 0 To 99
  Debug Str(*pointer\p[i]) + " -> " + Str(*pointer\p[i]\b)
Next
:D
or do i have get something wrong? :oops:
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
I think this is what luis said. I had the same problem many times.
It works, but it would be easier to use if PB could support this.

Re: *Pointer.<Type> with standard types

Posted: Sun Nov 13, 2011 5:30 pm
by luis
DarkDragon wrote:Maybe it would be better to disallow standard types for pointers.
Yes, that's what was about to write but gardening took priority :)

In fact, in my understanding of pointers with PB...

*ptr it's a pointer pointing to some memory area, not addressable in a structured way through the same pointer, so you have to use the Peek[?] family of functions to read from it (or use a different pointer type)

*ptr.Integer is a pointer to an area you want/need to access in a structured way and containing a basic type of data (Integer, Long, etc.)

*ptr.STRUCTNAME is the same as above but using a user defined structure

*ptr.i, *ptr.b, *ptr.c, etc are simply meaningless, or if you really want to put it that way, equivalent to *ptr

So what suggested by Danilo *could* have a meaning (I understand your point of view), but I would found better to remove the meaningless definition altogether and stick to the explicit structured pointer definition.

Danilo wrote: I think this is what luis said. I had the same problem many times.
It works, but it would be easier to use if PB could support this.
Exactly. The C way would be perfect, but considering *name in pb is a name variable different from name probably it would be a little more difficult to implement it that way for Fred than, for example, something like this:

Code: Select all

; ascii mode

TEST$ = "01234567890"

*ptr.byte = @TEST$   

*ptr\b = Asc("@") ; this works, first byte changed

*ptr\b + 3 = Asc("@") ; this is meaningless now, it could be used to obtain what I was asking ?

Debug TEST$ 


Re: *Pointer.<Type> with standard types

Posted: Sun Nov 13, 2011 5:39 pm
by Danilo
luis wrote:

Code: Select all

; ascii mode

TEST$ = "01234567890"

*ptr.byte = @TEST$   

*ptr\b = Asc("@") ; this works, first byte changed

*ptr\b + 3 = Asc("@") ; this is meaningless now, it could be used to obtain what I was asking ?

Debug TEST$ 

Syntax wise

Code: Select all

*ptr\b + 3 = Asc("@") ; this is meaningless now, it could be used to obtain what I was asking ?
is not good, but with

Code: Select all

*ptr[3]\b = Asc("@") ; cool shit
it would be possible. It works same in C/C++ and it works good.

Re: *Pointer.<Type> with standard types

Posted: Sun Nov 13, 2011 5:50 pm
by luis
I know it's not proper, should be (*ptr + 3)\b =
I was only thinking it would be easier to implement, but your way with the [] probably is the most sensible way and not much harder to implement in the PB parser (I suppose).

EDIT: Yes, on second thought my syntax was really bad.

It's still a funny thing you can compile that code right now.

Thanks for not hitting me on the head talking about something a little different in your thread.

Re: *Pointer.<Type> with standard types

Posted: Sun Nov 13, 2011 6:27 pm
by Demivec
Danilo wrote:

Code: Select all

*ptr[3]\b = Asc("@") ; cool shit
it would be possible. It works same in C/C++ and it works good.
This would be a great thing to be able to use.

The way it is currently possible, as PMV's example demonstrated, is a bit clumsy and hard to read. It would be nice not to have to declare an additional structure for that kind of access.


I may be wrong but it doesn't seem like it would 'break' any older code if such a thing were implemented.