• Advertisement
Sign in to follow this  

Why are pointers frightening?

This topic is 4683 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
Quote:
why are pointers scary?




int array[10];
int * intptr = array;
int value = ask_user_for_item();
printf( "You chose item %d: %d\n", value, intptr[value] );
printf( "Setting it to 0.\n" );
intptr[value] = 0;



Now, if your routine ask_user_for_item() may return values < 0, or > 9, you have a bug in the program, where an arbitrary memory location can be first read, and then set to 0, and there's really no error checking that the compiler can do.

The basic scare with pointers is that a pointer doesn't come with a "minimum" and "maximum" value that you can compare/assert/validate against. If you have a bug that causes a pointer value (or offset) to go out of whack, arbitrary data in your program, entirely un-related to what the pointer is supposed to be doing, will get blasted. This makes for hard-to-find bugs.

Share this post


Link to post
Share on other sites
Things not to do with pointers.

#1, point to an arbitrary location in memory.
int *ptr;
ptr = 10000; //i'm not sure, but I think you might have to cast in order for this to work.

#2, not setting a pointer to NULL (or zero) when you create it, before you allocate memory.
#3, not setting a pointer to NULL after you destroy it.
#4, not checking for NULL before you use it.

Share this post


Link to post
Share on other sites
pointers can save a lot of memory - for example a pointer to the users current weapon ( in a games example) uses 4 bytes, without them it would be harder to see which current weapon there is etc.

In games i have found them invlauable for dynamics, bu cant really see much use in console apps

Share this post


Link to post
Share on other sites
Pointers merely depend on the situation, and I rather like them.
You would use the double pointer to an array of pointers for linked lists, as they have been previously stated. Linked lists are good if you are going to manipulating the order, size, etc., of an array of objects. If you move or delete something, instead of having to shift the whole slew of objects following, you would merely shift the 32-bit (for Windows) integral value instead.
In other words, the pointers can be used for simplicity, or efficiency.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Alpha_ProgDes

void function(int **param[])

how the hell do you read that anyway? is it: an array of pointer addresses? and how would you use it?


I'm assuming it's a vector with the last item in the array a null.

Share this post


Link to post
Share on other sites
Hi,

Pointers are frightening because people fear what they do not know.

Pointers are just an extension of basic Assembly language. In assembly you reference data using a base memory position where your data is stored (the DS segment) and you access a specific point using an offset ([DS]+5 for example). Almost all data access is done this way though memory directions. Pointers are just an extension of that. They are basic elements that help you to talk with the computer in its own language. Denying this, is just limiting yourself.

Even languages that self-proclaims as 'no pointer' languaje as Java (or .Net) use pointers. Why do you think you have to use the 'new' keyword? You are allocating memory for your sctructures and memory allocation requires a pointer to hold the address for that memory. They just mask if so it looks like no pointers are used... but its always the same.

Pointers aren't evil. That affirmation is just like saying a gun is evil. They are just a tool that placed in the right hands can create Doom3 or HalfLife2. Placen in wrong hands they can format your HDD.

Anyway, once you get used to them, they are a tool so useful that its almost impossible to live without them.

Luck!
Guimo

Share this post


Link to post
Share on other sites
Basically level 1 pointers can be handle without much problems, but when you start using higher level mean more dereferencing, or mixing with OOP and complex data structure then that's when problems can arrive. Mostly what everyone probably experienced is prob dereferencing a bad pointer and pretty much crash your program.

Share this post


Link to post
Share on other sites
Quote:
how the hell do you read that anyway? is it: an array of pointer addresses? and how would you use it?


its an array of pointers to pointers to ints, which is the same thing as ***int (if you know you are using the first pointer to access an array)

just like:

int main(int argc, char **argv){

is the same as

int main(int argc, char *argv[]){

with the slowest varying dimension represented by the []'s and being of size argc. argv is an array of arrays of characters, its just that C doesn't let you do argv[][].

Share this post


Link to post
Share on other sites
void function(int **param[]) is exactly the same thing as void function(int ***param)

Share this post


Link to post
Share on other sites
Pointers don't get scary until you reach this:
(*(void (*)( CharData *, char *))Cmds.DoFun) ( ch, Command );

About then is the right time to die.

Share this post


Link to post
Share on other sites
Quote:
Original post by OpenGL_Guru
why would you use double referencing though? is it really necessary?


DLLs would require you to use double referencing. Passing information between the boundary of the DLL.

Share this post


Link to post
Share on other sites
This may seem kind of stupid but the only reasion I had a hard time with pointers is, even though I knew C++ didn't really recognize whitespace, because I thought that:

int *ptr;
int * ptr;
int* ptr;


had each different meanings.

Share this post


Link to post
Share on other sites
Quote:
DLLs would require you to use double referencing. Passing information between the boundary of the DLL


That's wrong.

You use double indirection when you want to change the value of a pointer, and make it clear that's what you're doing. It's like using a reference, except you can change the value of the reference, not just the referenced-at item.

For example, to remove an item from a linked list:


struct Node {
Node * next;
int value;
};

bool remove_from( Node ** list, Node * node ) {
while( *list ) {
if( *list == node ) {
*list = node->next;
return true;
}
list = &(*list)->next;
}
return false;
}

Share this post


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

  • Advertisement