Pointers, PEEKing, and POKEing

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
User avatar
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Pointers, PEEKing, and POKEing

Post by Patz QuickBASIC Creations » Thu Aug 24, 2006 4:18 pm

Is there a way to make pointer variables in QBASIC? That would be handy for a GUI...

Also, can someone give me a quick overview of the pro's and con's of PEEK and POKE?

Gracias.

Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Re: Pointers, PEEKing, and POKEing

Post by Z!re » Thu Aug 24, 2006 5:33 pm

Patz QuickBASIC Creations wrote:Is there a way to make pointer variables in QBASIC? That would be handy for a GUI...
No, use integers or longs.
Also, can someone give me a quick overview of the pro's and con's of PEEK and POKE?
Pros:
-freedom
-speed
-power
Cons:
-can lead to bad crashes
-can corrupt your BIOS (if using pure DOS)
-some people find them ahrd to use

I'm sure someone will provide more pros and cons.
I have left this dump.

User avatar
Skyler
Coder
Posts: 16
Joined: Wed Jun 07, 2006 4:15 pm
Location: Delta Quadrant

Post by Skyler » Mon Aug 28, 2006 3:50 pm

Cons:
I dont know how to use them.
Pros:
There is an entry in the help file.
For God so loved the world he gave His only begotten Son that whosoever believeth on Him shall have everlasting life.
John 3:16

Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re » Tue Aug 29, 2006 10:30 am

Skyler wrote:Cons:
I dont know how to use them.
Pros:
There is an entry in the help file.
How useful. No, really, I'm not being sarcastic, I'm sure this is exactly the kind of reply Patz was looking for, keep up the amazing work!
I have left this dump.

User avatar
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Post by Patz QuickBASIC Creations » Tue Aug 29, 2006 4:11 pm

Z!re wrote:
Skyler wrote:Cons:
I dont know how to use them.
Pros:
There is an entry in the help file.
How useful. No, really, I'm not being sarcastic, I'm sure this is exactly the kind of reply Patz was looking for, keep up the amazing work!
I would have NEVER guessed to look in the online help. BRILLIANT!!!

Z!re, how exactly do they create crashes? Isn't there a way to allocate STACK space (I know the STACK and CLEAR commands) and get the memory addresses they use?

Also, how do they corrupt your BIOS? Don't want that to happen... :?

Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re » Wed Aug 30, 2006 7:34 am

Patz QuickBASIC Creations wrote:
Z!re wrote:
Skyler wrote:Cons:
I dont know how to use them.
Pros:
There is an entry in the help file.
How useful. No, really, I'm not being sarcastic, I'm sure this is exactly the kind of reply Patz was looking for, keep up the amazing work!
I would have NEVER guessed to look in the online help. BRILLIANT!!!

Z!re, how exactly do they create crashes? Isn't there a way to allocate STACK space (I know the STACK and CLEAR commands) and get the memory addresses they use?

Also, how do they corrupt your BIOS? Don't want that to happen... :?
QB's peek and poke can not only access memory, but "hardware" aswell (Actually, memory linked to the hardware, but lets be simple)
And the way you'd use them is to use a DIM and create an array, you'd then do something along the lines of:

Code: Select all

dim myPtr as long
dim myMemory(10000) as integer
myPtr = VARPTR(myMemory(0)

POKE myPtr+30000, 100  'because we dimed the array as integer, it actually holds 40'004 bytes, remember it start at 0

Print PEEK(myPtr+30000)
Keep in mind that QB is very limited when it comes to how much memory you can "allocate" (DIM)

Now, lets take the above example and make it nasty:

Code: Select all

dim myPtr as long
dim myMemory(100) as integer '404 bytes, once again, remember it starts at 0 and integer = 4 bytes, 101*4 = 404
myPtr = VARPTR(myMemory(0)

POKE myPtr+3000, 100

Print PEEK(myPtr+3000)
Now thats bad you're writing to memory that is "out of bounds" as in, outside what you have allocated. In a windows environment the program would be stopped by the OS and an error message shown. This is not the case with DOS.
In addition, POKEing to certain memory addresses could overwrite the BIOS settings.

To keep safe, you generally only PEEK/POKE memory that you created via DIM.
I have left this dump.

Post Reply