Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
i never use pointers
#1
i need a example code that how to work with memory pointers
Reply
#2
In QB? QB doesn't have intrinsic pointers - and you shouldn't need them. If you want, I can show you some code that emulates pointers (extremely slowly, kinda useless).[/code]
f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Reply
#3
show me then
Reply
#4
You should know first that this isn't used by anyone - it's just interesting to look at.
A pointer is a variable that points to another variable. It simply holds it's address in memory.
An address (with DOS, which is what we're dealing with) consists of two elements: a segment, and an address. A "segment" is just a little part of memory, and an offset is at what address IN that segment the variable is.
Code:
TYPE Pointer
    Segment AS INTEGER
    Offset AS INTEGER
END TYPE
DIM Var AS INTEGER
DIM PtrToVar AS POINTER
PtrToVar.Segment=VARSEG(Var)
PtrToVar.Offset=VARPTR(Var)
DEF SEG=PtrToVar.Segment
POKE PtrToVar.Offset,5
DEF SEG
PRINT Var
That only works with one byte. However, with a bit of juggling, you could get it to work with the whole integer.
Clumsy though, eh?
f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Reply
#5
Quote:[...]Clumsy though, eh?
huh, yah it is
Reply
#6
Well and you don't even see the main advantage of pointers in that example... which is that you can dynamically allocate a piece of memory and then point to it.

Picture this: Rather than having to know in advance exactly how much memory your program will need for sprites, you simply allocate it on the fly as you load in your data. Have a linked list of general pointers that you can stick your sprite info into. Not only that, but with a pointer it gets allocated on the heap instead of the stack.
Reply
#7
Quote:Well and you don't even see the main advantage of pointers in that example... which is that you can dynamically allocate a piece of memory and then point to it.

Picture this: Rather than having to know in advance exactly how much memory your program will need for sprites, you simply allocate it on the fly as you load in your data. Have a linked list of general pointers that you can stick your sprite info into. Not only that, but with a pointer it gets allocated on the heap instead of the stack.
Yup. The almighty malloc(). Big Grin
f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Reply
#8
... which is what REDIM is for... :-?

But pointers (and approximations in QB using PEEK/POKE) are also useful for optimization purposes; there's a large variety of tricks and shortcuts that can be used with pointers. Also, in QB, "pointers" can be used to avoid array bounds checking.

Therefore, pointers are not useless in QB. Smile
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)