What are some applications for pointers?

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
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

What are some applications for pointers?

Post by Mentat »

I know how to use them, but what can they do that I normally can't do without them?
For any grievances posted above, I blame whoever is in charge . . .
User avatar
Codemss
Veteran
Posts: 124
Joined: Sun Jun 24, 2007 6:49 am
Location: Utrecht, The Netherlands
Contact:

Post by Codemss »

They give you more control over the memory. I also believe that they are faster. They don't make new things possible, I believe.
Check out my site: <a href="http://members.lycos.nl/rubynl">Click here</a>
Hope you like it. Send some feedback if you want: <a href="mailto:basicallybest@live.nl">Mail me</a>
Codemss, before known as RubyNL
User avatar
Stoves
Veteran
Posts: 101
Joined: Fri Feb 10, 2006 12:24 am
Location: Nashville, TN

Post by Stoves »

Codemss nailed the essential purpose of pointers: memory management and speed. There's several practical uses for pointers. Here's two sites that discuss the use of pointers: (C++ is the context, but it applies to FB as well)

http://publib.boulder.ibm.com/infocente ... ptrdec.htm

https://www.cs.tcd.ie/Martin.Emms/NLP/C ... ode54.html

Personally speaking, I find pointers especially helpful in saving program memory. Instead of functions creating copies of variables every time they're called, you can just pass the address of the variable or complex data type to the function so that the function can manipulate the variable directly.

One thing that is unique to pointers is that you can actually have pointers to pointers. This actually presents the potential for wasting memory, but it's got some useful applications that can't really be easily performed without pointers. See the following links for more info on pointers to pointers.

http://www.eskimo.com/~scs/cclass/int/sx8.html

http://computer.howstuffworks.com/c32.htm
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

memory

Post by burger2227 »

Another way to manage memory is to make your function or sub variables STATIC. That way, the variable is erased on exit.

Ted
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Post by Mentat »

Thanks
For any grievances posted above, I blame whoever is in charge . . .
Nemesis
Coder
Posts: 36
Joined: Mon Aug 16, 2004 12:54 pm
Location: Colorado Springs, Colorado

Re: memory

Post by Nemesis »

burger2227 wrote:Another way to manage memory is to make your function or sub variables STATIC. That way, the variable is erased on exit.

Ted
I always thought STATIC preserved the value stored in the variable if the procedure or function is exited, then called again.
Correct me if I'm wrong.
MystikShadows
Veteran
Posts: 703
Joined: Sun Nov 14, 2004 7:36 am
Contact:

Post by MystikShadows »

So did I. I wonder, in other languages, if it's not rather equal to dereferencing and rereferencing a memory location. hence that location doesn't change it's value, it's just not accessed anymore instead. untill it gets back to the sub where it's reassigned to the memory location. Hence, doesn't loose it's value.
When God created light, so too was born, the first Shadow!

MystikShadows

Need hosting? http://www.jc-hosting.net

Interested in Text & ASCII development? Look no further!
http://www.ascii-world.com
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

sorry

Post by burger2227 »

I meant that the location never changes so that the variable is just not floating in space like normal Sub or Function variables are. Usually despite keeping the value, it can be changed. The location does not.

Ted
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
User avatar
Michael Calkins
Veteran
Posts: 76
Joined: Tue Apr 05, 2005 8:40 pm
Location: Floresville, Texas
Contact:

Post by Michael Calkins »

C passes byval by default. QBASIC passes byref by default. References are similar to pointers in the context of this discussion.

mysub myvariable

passes myvariable by reference. If the sub changes it, it is changed for the caller as well. myvariable must be of the appropriate type.

mysub (myvariable) 'contained in parentheses

it becomes an expression that is passed by value instead of a variable passed by reference. a the value is stored in a local variable for the sub. myvariable is not being passed, so does not have to be of the same type.

Ordinarily, a procedure's local variables fall out of scope when the procedure returns. When a SUB is STATIC, its local variables do not fall out of scope, and are not erased between calls.

Be careful about relying on the addresses of variables remaining fixed. QBASIC can and does move string descriptors. I don't know if it moves numeric variables...

http://support.microsoft.com/kb/12337/en-us

The article seems to imply that string descriptors are contiguous with the strings themselves, and I don't believe they always are. There are other interesting knowledge base articles on related subjects.

here are some other good articles:
http://support.microsoft.com/kb/51501/en-us
http://support.microsoft.com/kb/71275/en-us

Regards,
Michael
User avatar
Kiyotewolf
Veteran
Posts: 96
Joined: Tue Apr 01, 2008 11:38 pm

pointers..

Post by Kiyotewolf »

i have ran into pointers in Turbo Pascal and Turbo Pascal windows flavor.. and hated every chance I ran into them..

omg.. those things make my head spin..

i 'ate em... oi..

<.<

Too bad I'm going to have to suffer and learn to use them cause they are in Freebasic now..

Kiyote
Banana phone! We need more lemon pledge. * exploding fist of iced tea! * I see your psycho cat and counter with a duck that has a broken leg, in a cast.
Post Reply