Page 1 of 1

What are some applications for pointers?

Posted: Tue Jan 08, 2008 7:13 am
by Mentat
I know how to use them, but what can they do that I normally can't do without them?

Posted: Sat Jan 12, 2008 10:34 am
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.

Posted: Fri Feb 15, 2008 11:52 pm
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

memory

Posted: Sat Feb 16, 2008 5:42 am
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

Posted: Sat Feb 16, 2008 8:08 am
by Mentat
Thanks

Re: memory

Posted: Sat Feb 16, 2008 4:31 pm
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.

Posted: Sat Feb 16, 2008 4:58 pm
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.

sorry

Posted: Sat Feb 16, 2008 5:19 pm
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

Posted: Mon Mar 24, 2008 1:15 am
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

pointers..

Posted: Mon Apr 07, 2008 12:34 am
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