Memory Random Access through Pointers

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
freedimension
Enthusiast
Enthusiast
Posts: 613
Joined: Tue May 06, 2003 2:50 pm
Location: Germany
Contact:

Memory Random Access through Pointers

Post by freedimension »

Like this (or some other Syntax, it's just an example)

*wptr.WORD
*wptr[10]\w

to change the 10th Word beginning with the adress of *wptr (i.e. *wptr + 10 * SizeOf(WORD))
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

like this?

Code: Select all

Structure WORDArray
  w.w[0]
EndStructure

Dim word.w(100)
word(25) = 1234


*wptr.WORDArray = @word()

Debug *wptr\w[25]
quidquid Latine dictum sit altum videtur
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Post by Danilo »

freak wrote:

Code: Select all

Structure WORDArray
  w.w[0]
EndStructure
Whats that ?

Array.type[0] isnt mentioned in the help, so its not official.
0 = NULL = Nothing.

Code: Select all

Structure WORDArray1
  w.w[0]
EndStructure

Structure WORDArray2
  w.w[1]
EndStructure

Debug SizeOf(WORDArray1) ; shows 2
Debug SizeOf(WORDArray2) ; shows 2

x.WORDArray1
x\w[10000] = 1 ; debugger doesnt warn

y.WORDArray2
y\w[00001] = 1 ; debugger warns, OK
Anyway, what freedimension probably meant is pointer stuff like
in C, so you can access every pointer also as an array of the
pointer types.
Would be a nice feature, and with freedimension's syntax it would
be fine: *p[10]\structure_member = 12
With freaks way, you have to declare a new structure first.
cya,
...Danilo
...:-=< http://codedan.net/work >=-:...
-= FaceBook.com/DaniloKrahn =-
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

Danilo wrote:
freak wrote:

Code: Select all

Structure WORDArray
  w.w[0]
EndStructure
Whats that ?

Array.type[0] isnt mentioned in the help, so its not official.
0 = NULL = Nothing.
It's not in the manual because it's not been updated. I asked Fred on IRC about keeping this feature because I use it a lot. I don't mind if it changes to a cleaner syntax, but please don't remove it without giving us a replacement (which doesn't involve doing things like "PeekL(*pointer + index * sizeof(type), data)").

Fred made the point about why not just use a structure with 99999999 items instead of zero. But then you can't embed the zero length array in other structures and still be able to use sizeof sensibly.

And why even use the square brackets for this purpose if it were to get cleaned up? Why not just use the round brackets and have the ability to manually set the array base rather than having to use Dim to create a new one each time?
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Post by Danilo »

Its all messed up and totally inconsistent and inconsequent.
This has absolutely nothing to do with good and clean
programming language design. Its an illogical mix-up on every
corner and its all confusing.

See this:

Code: Select all

Structure xyz
  *p.POINT
EndStructure

a.xyz
a\*p = AllocateMemory(1,SizeOf(POINT)) ; SYNTAX ERROR
Syntax Error, fine.

Now i´ve seen people using it without the '*':

Code: Select all

Structure xyz
  *p.POINT
EndStructure

a.xyz
a\p  = AllocateMemory(1,SizeOf(POINT)) ; OK, Works
Thats not logical, because PB doesnt use pointers like C where
you specify '*' sometimes and sometimes not.

OK, let us continue:

Code: Select all

Structure xyz
  *p.POINT
EndStructure

a.xyz
a\p  = AllocateMemory(1,SizeOf(POINT)) ; OK, Works

a\p\x = 12
Writing seems to work... doesnt crash here.

Let us test reading:

Code: Select all

Structure xyz
  *p.POINT
EndStructure

a.xyz
a\p  = AllocateMemory(1,SizeOf(POINT)) ; OK, Works

Debug a\p\x ; reading works
Test successful, works.

Now lets test both together, reading and writing:

Code: Select all

Structure xyz
  *p.POINT
EndStructure

a.xyz
a\p  = AllocateMemory(1,SizeOf(POINT)) ; OK, Works

a\p\x = 12  ; ??

Debug a\p\x ; ??
Ooohhh.. it crashes with debugger enabled. With disabled
debugger it doesnt crash. Hmm, OK.
If i make the last line (Debug..) a comment, it doesnt crash
anymore.

Lets try to exchange the last 2 lines, maybe it works... :lol:

Code: Select all

Structure xyz
  *p.POINT
EndStructure

a.xyz
a\p  = AllocateMemory(1,SizeOf(POINT)) ; OK, Works

Debug a\p\x ; ??

a\p\x = 12  ; ??
Oi! Now the debug line doesnt crash, but the debugger says
"pointer is 0" on the last line.

I´m confused, nothing works. And now the 0-Array length ??
Whats that ?? A word-array with length 0 is nothing, but
debugger says its 2 bytes long.

Code: Select all

Structure WORDArray 
  w.w[10]
EndStructure

p.WORDArray
p\w[0]      = 12
p\w[100000] = 8000 ; debugger gives error,
                   ; array index out of bounds.
Thats the correct way, works.

Now the wrong way:

Code: Select all

Structure WORDArray 
  w.w[0] 
EndStructure

p.WORDArray
p\w[0]      = 12
p\w[100000] = 8000
...just crashes.

There is absolutely no logic and consistency in this stuff, sorry.

An array with size 0 is NOTHING. 0 = NULL = NOTHING.

Its magic... in PB 'nothing' is 2 bytes long if 'nothing' is a word. :lol:

There is another thread in the forum that shows that pointers
dont work correctly because all is messed up, especially with
pointers in structures.

A simple '*p.LONG = malloc(1000)' and '*p + 4' works fine, but
thats all...

And now a little brain-teaser for the smart guys:

Code: Select all

Structure xyz
  *p.POINT
   p.SIZE
EndStructure

a.xyz
a\p = AllocateMemory(1,SizeOf(POINT))
The question is, which 'p' is accessed in the last line, the
pointer '*p' or 'p' ??


All this isnt in the manual because its all messed up, thats it. :cry:
cya,
...Danilo
...:-=< http://codedan.net/work >=-:...
-= FaceBook.com/DaniloKrahn =-
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

The bugs and inconsistencies you mention are separate problems from the (ab)use of the "special feature" that is zero length arrays ;)

It's currently the only way to access arrays of variable length via pointers, without going to the hassle of:
- Using peek, poke and calculating the addresses yourself or
- Changing the 0 in the structure to some unfeasibly large number. Which makes the bounds checking as pointless as using 0.

I don't care how it's done or if the syntax changes, I only need the ability to access arrays by pointers without much hassle.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Post by Danilo »

tinman wrote:I don't care how it's done or if the syntax changes,
I care about this because a language should be designed clean
and consistent.
At this time it looks more like the pointer stuff is hacked together
without following the general PB language style.
tinman wrote:I only need the ability to access arrays by pointers without
much hassle.
freedimension's wish is here to achieve this. :wink:
cya,
...Danilo
...:-=< http://codedan.net/work >=-:...
-= FaceBook.com/DaniloKrahn =-
freedimension
Enthusiast
Enthusiast
Posts: 613
Joined: Tue May 06, 2003 2:50 pm
Location: Germany
Contact:

Post by freedimension »

Danilo wrote:
tinman wrote:I don't care how it's done or if the syntax changes,
I care about this because a language should be designed clean
and consistent.
At this time it looks more like the pointer stuff is hacked together
without following the general PB language style.
Yes, there are some inconsistencies in there, but also I like some of the pointer stuff in PureBasic though it's different than in any other language I know. You have to get used to it, but that's ok for me.
For example, I like the way, Pointers always have the same way of writing, always with the asterisk in front of it. No hazzle like in C as to what this is, a variable or a pointer. Also the fact that you can only manipulate the underlying values through a structure, is IMHO a plus and not a minus. But I have to commit that at first I was a little bit startled with this Syntax, but now, as I regularly use it, it becomes clearer and clearer, and it's so much easier to use than C pointers.
Post Reply