Jump to content
  • Advertisement
Sign in to follow this  
JeremyWB

Why are pointers frightening?

This topic is 4861 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
To create advanced data structures such as trees and lists, to pass arguments to funtions by reference, and more.

Share this post


Link to post
Share on other sites
Or to just dynamically create an array.

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 =)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
imho, pointers are not so bad, it is when you start seeing things like:
void function(int **param[])
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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#.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!