6.0 Those Evil Pointers
The most powerful tool for any programmer is also the most feared…but it doesn’t havta be. Pointers are something you’ve been using ever since you started making variables in one way or another. Its just a way to make variables work exactly the way you need to make them work, and make them work as fast as possible.
What are pointers? Well, think back to the maps we drew about the arrays. Remember how each array had an “origin” that pointed to another location in memory that started the array of memory? Pointers work the same way, except that they don’t get assigned memory spaces, UNTIL the program is ACTUALLY RUN. Think about it..how often do you know exactly how many you will need at any one time. If you setup a program to load 20 budget items, how much memory do you waste only doing 8? What happens if you suddenly need 25 items? Pointers allow you to declair how much you need at the time you actually run it.
So why do people get so scared of pointers? Pointers can make your programs work fast, efficient and all that catnip & fuzzy mice stuff…IF they are done right. If they are done wrong…well…bad things happen…catnip gives you bad trip….fuzzy mice bite back…etc. So…the answer is: Do Pointers Right. > ^.^< m
6.1 Pointer Basics
The basic start for creating a pointer is to use the standard pointer reference. In C/C++, it’s the “*” astarisk next to the declairation.
char* names;
int* values;
In Pascal, the pointer is done with the “^” caret after the variable.
Names: ^String;
Values: ^Integer;
6.2 WC for Computers
Now once you’ve declaired a variable of this type, you have an empty address…remember…all pointers do is store addresses, so you can’t try to use them until you put an address in them. This works by telling the computer to take a small section of memory of the size you want, and reserve it for the use of your program. This allocation of memory is the source of most bugs in many programs, mainly because of something called a “Memory Leak”--basically, when programs tells the operating system to reserve bits of memory, but never releases that memory, then all of a sudden your computer has a bunch of memory that can’t be used by anything until it is fixed. Some programming languages like Java and Smalltalk use something called “Garbage Collection” where the actual cleanup of unused memory is taken care of by other systems. This is very debatable which way is better because nobody can agree on the “best” way to do garbage collection and the whole garbage collection system ALWAYS makes things much slower and visiable to the user. Essentially, every programmer must know both ways in order to be able to get the most outta their programs.
6.3 Memory Dynamics
To give your pointer variables substance, you have to tell the computer what you want to create, and how many. A single integer for instance would look something like this (in C++):
Int* x;
x = new int;
….. do something…..
delete x;
The keyword “new” creates the new memory space based upon the size of the variable. After you use the variable, the keyword “delete” cleans up the memory. To create an array of items, simply add a demention parameter just like you would in array.
char* names;
names = new char[200];
…. Do something ….
delete [] names;
the extra “[]” tells the compiler to remove the array.
6.4 Alternate Addressing
One odd thing about pointers is that you generally have to use them differently than other types of variables. Normal variables are setup to give you direct access to the contents; there’s no need to do anything special to get to the contents of the memory. However, Pointers are automatically addresses that will not even have memory spaces until you setup some memory AND assign it for it to use. Therefore, pointers must be used in a different way.
Consider:
int* x;
x = new int;
we’ve got an integer pointer, and it has memory. If you did a simple “cout”
cout < < x;
you would get a hexadecimal number that is the actual address of the variable. But if you want to do something with it, you have “dereference” the variable. There are bunches of different ways to do this. For our integer (and most single variables) in C++ it looks like this:
*x=100;
cout < < *x;
And…you’ll get what you’re after. For structures, you need to access each member with an arrow (-> ) instead of a dot. SO, if you consider this:
struct data{
int a;
int b;
}
data one;
data *two;
one.a = 20;
one.b = 30;
two = new data;
two-> a = 20;
two-> b = 30;
delete two;
Each language is different; so double-check each kind in your quick reference manual.
6.5 Secret Pointer Plots
You can do many different wonderful things with pointers. There are millions of mathmatitians and computer scientists thinking up new algorithms (here’s the offical definition: Algorithm n : a precise rule (or set of rules) specifying how to solve some problem [syn: algorithmic rule, algorithmic program]. Basically, an “algorithm” is the way you describe how to do something but using a few details as possible. Kinda like describing how to make a tuna fish sandwitch without using the words “tuna”, “bread” or “GIMMIE MY TUNA FISH BACK”). These algorithms make things work out a lot easier and faster, and there are lots available. But the most common ones are great to learn and very useful.
“Linked Lists” are a combination of structures and pointers to give you a complex list of data in memory that you can grow and shrink without any problems. There are sooo many versions and applications of this idea that it’s very important to try to understand the basic idea. The idea is that you create a structure with all of your information, then you add in a pointer to another instance of the same structure. Then, you create a bunch of these, with each structure pointing to the next copy of the structure until you have all the information you need.
[Info ]
[Pointer]---------> [Info ]
[Pointer]---------> [Info ]
[Pointer]---------> NULL
and so on. By using a pointer inside the structure, there is no reason or need to declair a bunch of variables. You declair one copy in your program, but you then can use the “new” command (or whatever command is right for your language) and reuse the pointer inside the structure itself.
6.5.1 OOO…look what I found
Source: The Free On-line Dictionary of Computing (2003-OCT-10)
algorithm
<> A detailed sequence of actions to perform to accomplish some task. Named after an Iranian mathematician, Al-Khawarizmi. Technically, an algorithm must reach a result after a finite number of steps, thus ruling out brute force search methods for certain problems, though some might claim that brute force search was also a valid (generic) algorithm. The term is also used loosely for any sequence of actions (which may or may not terminate).
Paul E. Black's Dictionary of Algorithms, Data Structures, and Problems. (2002-02-05)
6.6 Link List Example
#include <>
struct node{
int data;
node* ptr;
};
main()
{
// a "Cursor" is a pointer to some point in the list,
// so you dont get lost. Temporary variables like
// this are often used.
node* myinfo;
node* cursor;
int x;
myinfo = new node;
cursor = myinfo;
for( x=100; x< 200; x++ )
{
// normally, link lists will end with a "NULL" to tell the ending;
// we're just using a counter, so we know how many.
myinfo-> data = x;
myinfo-> ptr = new node;
myinfo = myinfo-> ptr;
myinfo = NULL;
}
myinfo = cursor;
for ( x=100; x< 200; x++ )
{
cout < <> data < < "\n";
myinfo = myinfo-> ptr;
// clean up as we walk through
delete cursor;
cursor = myinfo;
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment