*Pointer.<Type> with standard types

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

*Pointer.<Type> with standard types

Post 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.
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: *Pointer.<Type> with standard types

Post by Shield »

Seems like a nice idea, +1. :)
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
User avatar
IceSoft
Addict
Addict
Posts: 1682
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Re: *Pointer.<Type> with standard types

Post by IceSoft »

Belive! C++ version of Puzzle of Mystralia
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: *Pointer.<Type> with standard types

Post 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.
PMV
Enthusiast
Enthusiast
Posts: 727
Joined: Sat Feb 24, 2007 3:15 pm
Location: Germany

Re: *Pointer.<Type> with standard types

Post 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
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: *Pointer.<Type> with standard types

Post 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.
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: *Pointer.<Type> with standard types

Post 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 :)
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: *Pointer.<Type> with standard types

Post 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. :)
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
PMV
Enthusiast
Enthusiast
Posts: 727
Joined: Sat Feb 24, 2007 3:15 pm
Location: Germany

Re: *Pointer.<Type> with standard types

Post 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
DarkDragon
Addict
Addict
Posts: 2344
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: *Pointer.<Type> with standard types

Post 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.
bye,
Daniel
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: *Pointer.<Type> with standard types

Post 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.
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: *Pointer.<Type> with standard types

Post 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$ 

Last edited by luis on Sun Nov 13, 2011 5:40 pm, edited 1 time in total.
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: *Pointer.<Type> with standard types

Post 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.
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: *Pointer.<Type> with standard types

Post 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.
"Have you tried turning it off and on again ?"
A little PureBasic review
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 »

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