Why are pointers frightening?
So, they hold the memory address of a variable, why is that frightening? I suppose it could be because it's not entirely evident what pointers are "good for"? What purpose do they serve?
To create advanced data structures such as trees and lists, to pass arguments to funtions by reference, and more.
Or to just dynamically create an array.
You recognize this?
Let's break it down =)
int *arr is a pointer of the type int.
new will tell your OS that "i want a place in memory where i can store 10 ints, go fetch!".
The OS responds "Ahh, easy! Here you go, all set up. And here is the start of that memory area!"
...and voilla, you have a pointer returned by new.
Now we just let the arr pointer store the value returned but new and yay! We're happy!
Now another example. A sort of linked list...
A linked list is not like a normal array, where all values come after each other...No, each node keeps track of what node comes after themself.
A third example...
int arr[10] = {21,32,43,54,65,76,87,98,11,22};
Ok, so what if we want a pointer to point on say the last element in the array?
Easy!
int *arrPtr = &(arr[9]);
...and since you know that an array is stored continously, you can do this also:
int *arrPtr = arr + 9;
If you print out arrPtr it will, amazingly, print out 22 =)
So, pointers isn't something to be afraid of, but a certain amount of respect when dealing with them is recommended.
Say for instance you point to some area where you ain't supposed to be pointing to. Right, so the OS will prevent most areas that dosn't belong to your program, but what if you create an array dynamically, and then store something else in that pointer? Your array will be lost forever in the void and you wouldn't know about it. You may find out when your program crashes up in your face because it can't allocate more memory...or worse things can happen...
I hope this was enlightning for you..and if not, beat on me and i'll try better =)
You recognize this?
int *arr = new int[10];
Let's break it down =)
int *arr is a pointer of the type int.
new will tell your OS that "i want a place in memory where i can store 10 ints, go fetch!".
The OS responds "Ahh, easy! Here you go, all set up. And here is the start of that memory area!"
...and voilla, you have a pointer returned by new.
Now we just let the arr pointer store the value returned but new and yay! We're happy!
Now another example. A sort of linked list...
A linked list is not like a normal array, where all values come after each other...No, each node keeps track of what node comes after themself.
class Node{ int value; Node *nextNode;};// ok, so what now?Node *start = new Node;start->value = 15136;start->nextNode = new Node;start->nextNode->value = 123151;start->nextNode->nextNode = new Node;start->nextNode->nextNode->value ....// and so it goes on...// So each pointer just point to the place in memory where it knows the next node is located
A third example...
int arr[10] = {21,32,43,54,65,76,87,98,11,22};
Ok, so what if we want a pointer to point on say the last element in the array?
Easy!
int *arrPtr = &(arr[9]);
...and since you know that an array is stored continously, you can do this also:
int *arrPtr = arr + 9;
If you print out arrPtr it will, amazingly, print out 22 =)
So, pointers isn't something to be afraid of, but a certain amount of respect when dealing with them is recommended.
Say for instance you point to some area where you ain't supposed to be pointing to. Right, so the OS will prevent most areas that dosn't belong to your program, but what if you create an array dynamically, and then store something else in that pointer? Your array will be lost forever in the void and you wouldn't know about it. You may find out when your program crashes up in your face because it can't allocate more memory...or worse things can happen...
I hope this was enlightning for you..and if not, beat on me and i'll try better =)
Manipulating addresses is seen as a low level thing to do, also things like using a pointer that doesn't point to anything causes problems.
Cannot forget about when you start passing pointers around like crazy and your not sure if you are supposed to use -> or . and not to forget casting them as pointers in the first place. Ahhhh, it can be a real mess!
imho, pointers are not so bad, it is when you start seeing things like: that pointers can get messy. even then it is just more about knowing wtf is going on in the software component you are working on, than pointers are evil.
void function(int **param[])
Nope, pointers are the worst evil!
And the sad thing is that to make anything sophisticated you have to use them, which I suppose is pretty rewarding.
I finally know how to use them but they remain from hell
And the sad thing is that to make anything sophisticated you have to use them, which I suppose is pretty rewarding.
I finally know how to use them but they remain from hell
Pointers may be frightening to the beginner programer simply because they are so incredibly easy to misuse and offer the greatest chance of introducing bugs within your program. When used properly however, pointers are incredibly useful and serve many purposes within programming.
A world with pointers, is chaos.
(++ if anyone knows where I bastardised that quote from [smile]) Anyway, I could not live without pointers. When you get into advanced stuff, you eat, drink and breath pointers (not to mention templates, but that's another matter), and ya, without them, anything remotely complicated or intricate would be nigh on impossible. I love them, and I feel that anyone who doesn't have a solid grasp on them and their uses should put aside whatever they're doing and learn!
(++ if anyone knows where I bastardised that quote from [smile]) Anyway, I could not live without pointers. When you get into advanced stuff, you eat, drink and breath pointers (not to mention templates, but that's another matter), and ya, without them, anything remotely complicated or intricate would be nigh on impossible. I love them, and I feel that anyone who doesn't have a solid grasp on them and their uses should put aside whatever they're doing and learn!
They are frightening because they allow the programmer to actually make code that is close to assembly code. If it is close to assembly, it is close to the hardware. And, if you mess up with a pointer, you mess up your program. Other languages like Java and C# discourage pointer use. I'm not sure if Java has pointers, but I know that you have to use "unsafe mode" in order to use pointers in C#.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement