question about pointers

Hello,
I realized that I actually never use pointers in my codes. Now I was wondering is that something bad? Or do pointers just make the code more efficient? Or does it improves readability of the code? Or is there another reason why I should (not) use it ?
Any help is welcome.
See my post in this thread:

http://cplusplus.com/forum/general/105593/

for some reasons to use pointers.

If none of those reasons apply to the code you're writing, then there's no need to use them.
Last edited on
It really depends on your code. Pointers are useful in some instances and not in others.

For example, you may need to make use of heap memory. In that case, pointers are essential.
SomeClass *instance = new SomeClass();

Pointers can make the code more efficient too, yes. For example, passing a pointer to (or, for that matter, a reference of) a large structure bears a smaller overhead then passing the class itself.
Well I guess I don't need it for the most of those things if I just use a vector.
Thanks for the replies anyway.
If you're using vectors you might still benefit from passing them by reference. Granted, they'd probably have to be pretty big vectors to see a significant improvement.

Still, it's good practice in my eyes. I generally try to use references where possible.

void SomeFunc( const std::vector<int> &vec );
Well I guess I don't need it for the most of those things if I just use a vector.

I'm not sure how "using a vector" avoids the use of pointers in most of those situations I posted. Could you elaborate?
pointers are awesome! very usefull for getting rid of exceptional situations.

example.

say you have three nodes in a list.

1
2
3
4
struct Node{
   int data;
   Node * next;
};


1
2
3
4
5
6
Node n1, n2, n3;
//data doesn't matter
n1.next = &n2;
n2.next = &n3;
n3.next = NULL;
Node * head = &n1;


now lets say you want to iterate over them and erase the n2 node.
for that you need to make n1 point to n3.

you have three choices.
1) use two runners to always remember who the previous node is to erase it
2) use a pointer to a pointer to a node
3) add a Node * prev; to the Node struct

in the first case you have issues if the node you want to erase is the first one, which is at least one additional if to the loop which runs on the code, unless you want to use a dummy Node or some other trick which poses other issues under curtain circumstances.

in the third case your wasting memory you don't have to waste.

the second case has the "cleanest" code by me.

1
2
3
4
5
6
7
8
9
Node ** runner = &head;
while( *runner != NULL ){
   if( (*runner)->data == BAD_DATA ){
      *runner = (*runner)->next;
       //delete old node if you want
       continue;
   }
   runner = &(*runner)->next;
}


notice that this code would work if you would need to erase the first node or the last, you don't need any additional ifs for exceptional situations, no need to maintain prev pointers, or dummy nodes or any thing like that, however i'm not sure efficiency wise is the best way to iterate over a list while deleting stuff from it.
Last edited on
Topic archived. No new replies allowed.