PDA

View Full Version : Use/Misuse of Pointers



ct
7th May 2007, 18:10
Could anyone point out what are the advantages/disadvantages of using pointers. Also when should a programmer always try to use pointers and when should he try to avoid using pointers.

Thanks.

high_flyer
7th May 2007, 23:32
The short answer (since the long one is beyond the scope of this forum):
As long as you can do something without pointers in a convenient way - do it without pointers.
Otherwise - use pointers - but be careful.

wysota
8th May 2007, 01:26
Just don't force avoiding pointers. If it seems natural to use a pointer, do it. Just remember that copying a pointer doesn't imply copying the object pointed by it.

ct
8th May 2007, 06:38
The short answer (since the long one is beyond the scope of this forum):
As long as you can do something without pointers in a convenient way - do it without pointers.
Otherwise - use pointers - but be careful.

Well if you think hard you can do most of the things without pointers and there is less hassle has you don't have to allocate and free the memory. Moreover, using references can solve some of the things that can be done by pointers. So, guess what I was asking was the long answer. Any help would be appreciated.


If it seems natural to use a pointer, do it.

Could you please specify in detail at least some of the scenarios when it would seem natural/ unnatural to use a pointer.

high_flyer
8th May 2007, 09:33
Could you please specify in detail at least some of the scenarios when it would seem natural/ unnatural to use a pointer.
Polymorphisem is implemented with pointers for example.
Reference is bound to its object, so every time you want to have the same reference point to other objects you use a pointer.
The long answer can't be answered here, all though wysotas answer and mine do gove it in a nutshell.
You should read some material on the subject.

wysota
8th May 2007, 10:52
Could you please specify in detail at least some of the scenarios when it would seem natural/ unnatural to use a pointer.

It's natural to use a pointer for linked lists or other objects which may be moved in memory. It's faster to copy pointers than to copy objects.

It's unnatural (in C++) to use pointers as an argument to function/method if you intend to change the object in the function. Use references instead.

Using pointers is nothing bad (although some people think different), so if you know how to work with them, you can use them whenever you want and ignore people saying that PointersAreBad(C).

ct
8th May 2007, 16:32
I was kind of wondering, is pointers related to garbage collection ?? Although, I haven't yet come across a point where I need to delete the garbage because of memory overload, but could it be one possible usage.

I like to put things literally so,



void myfunc()
{
struct mypointer* ptr;

//...

//...

//..a long long function
}


in case if we have a long function with many variables then all of their scope will last until the function ends and might cause memory overflow, however if we use pointers then we could free the memory ourselves so there will be less memory load. However, I have yet to come across some scenario, is it a possibility or am I thinking too much :D

high_flyer
8th May 2007, 17:08
I was kind of wondering, is pointers related to garbage collection ??
Related in what way?
Pointer which are holding memory that is no more used - AND that was dynaically allocated are pointer to grabage, and should be deleted.
Refering to your example, allocating on the stack is limited due to the limited stack size.
So it is very possible (hapanned to me) to allocate more than the stack can give - then you have to use dynamic allocation (pointers) or resize the stack.
It doesn't have to me many variables, it can be just one - but big, say a char array to hold a 16MB image...

ct
8th May 2007, 20:51
hmm..let me get this right... if I have a character array which holds a 16 mb image data then it is going to be a problem because the array won't be destroyed until the end of the function right ?

but if we use a character pointer that holds the same amount of data then we have the flexibility of deleting the garbage data within the function itself and we don't have to wait till the end of the function...

I was kind of relating pointers to garbage collection in the sense that if a pointer is no longer used on the later half the function then we could free the memory of that pointer within the function i.e. an act of garbage collection. However if we have same amount of huge data say an array of 16 mb then we have to wait till the end of the function hence we cannot garbage collect...

However I am a bit confused about dynamic allocation of stacks. Do you mean that if I have big variable( say array) having around 32mb then I cannot create other variables and have to create pointers and allocate the memory ?

wysota
8th May 2007, 21:07
hmm..let me get this right... if I have a character array which holds a 16 mb image data then it is going to be a problem because the array won't be destroyed until the end of the function right ?
No, it might be a problem because on some architectures you'll be trying to use more memory than it's available for you.


but if we use a character pointer that holds the same amount of data then we have the flexibility of deleting the garbage data within the function itself and we don't have to wait till the end of the function...
In general one uses pointers when he wants the variable to survive the end of the function, not to be destroyed earlier. You can achieve the same by using the stack (at least in theory) by using scopes:

void fn(){
int var1;
{
int var2;
}
// var2 is no longer valid here
}


I was kind of relating pointers to garbage collection in the sense that if a pointer is no longer used on the later half the function then we could free the memory of that pointer within the function i.e. an act of garbage collection. However if we have same amount of huge data say an array of 16 mb then we have to wait till the end of the function hence we cannot garbage collect...
Garbage collection means that there is a mechanism which is able to track if a piece of memory is used or not and clean it up if possible without the programmer having to do it by himself. So the parent-child relationship in Qt can be treated as a very simple gc.


However I am a bit confused about dynamic allocation of stacks. Do you mean that if I have big variable( say array) having around 32mb then I cannot create other variables and have to create pointers and allocate the memory ?
The stack size is limited on many architectures (especially embedded ones), so you can't freely use up stack space - so actions like allocating large amount of variables or having a large number of recursions is not possible there.

marcel
8th May 2007, 21:09
Why are you so afraid of pointers?
Pointers are cool http://www.qtcentre.org/forum/images/icons/icon6.png!

Regarding stack size and stack overflow - take the case of recursive functions: are you willing to compute how many stack frames will you have and derive the number of vars and pointers that you must use, depending on the stack size? I doubt you can/will do that.

Therefore use pointers to avoid such issues and whenever you are in doubt.
For most it is common sense to know when to use pointers or allocate on the stack.

Regards

ct
8th May 2007, 21:21
aah..I got it now...dumb me..



1. void fn(){
2. int var1;
3. {
4. int var2;
5. }
6. // var2 is no longer valid here
7. }


and this is a neat operation, I had nearly forgotten about the block scope operator..thanx a lot wysota...

I will definitely use pointers when I feel necessary..ther very point I was asking this question was because I get all these notions from my classmates who are afraid of the site of pointers....

wysota
8th May 2007, 23:43
and this is a neat operation, I had nearly forgotten about the block scope operator..thanx a lot wysota...

Sure, but beware of the fact that this doesn't mean the variable is deallocated when it goes out of the inner scope. I'd assume it to be very compiler dependent, because the memory may be freed only when returning from the function, as this is done automatically by modifying the stack pointer in the CPU to reference the previous stack frame so no additional moves of the pointer are needed. So probably the memory is really freed when going out of inner scope on platforms that have a limited stack space. Otherwise it's just waste of CPU power, so the whole stack frame is probably freed in one go.